2

[更新]

我添加了 onCreateView 但现在我收到此错误:

07-03 14:39:36.878: E/AndroidRuntime(30913): FATAL EXCEPTION: main
07-03 14:39:36.878: E/AndroidRuntime(30913): java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
07-03 14:39:36.878: E/AndroidRuntime(30913):    at android.app.ListFragment.ensureList(ListFragment.java:402)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at android.app.ListFragment.onViewCreated(ListFragment.java:203)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at com.rss.alsace.AlsaceNewsFragment.onViewCreated(AlsaceNewsFragment.java:68)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:843)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at android.app.BackStackRecord.run(BackStackRecord.java:635)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at android.os.Handler.handleCallback(Handler.java:615)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at android.os.Looper.loop(Looper.java:137)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at android.app.ActivityThread.main(ActivityThread.java:4921)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at java.lang.reflect.Method.invokeNative(Native Method)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at java.lang.reflect.Method.invoke(Method.java:511)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
07-03 14:39:36.878: E/AndroidRuntime(30913):    at dalvik.system.NativeStart.main(Native Method)

我尝试在我的 android 应用程序中集成ActionBar-PullToRefresh 库https://github.com/chrisbanes/ActionBar-PullToRefresh )。

我的应用是用左侧的菜单(导航抽屉)构建的。当用户在菜单中选择一个项目时,我的应用程序的主面板上会显示一个片段。

每个片段都扩展 ListFragment。我今天尝试将 ActionBar-PullToRefresh 集成到这些片段中。

按照库的文档我必须将这些行集成到 onCreateView 中

 @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Inflate the layout
            View view = inflater.inflate(R.layout.layout_fragment, container, false);

            // The ScrollView is what we'll be listening to for refresh starts
            ScrollView scrollView = (ScrollView) view.findViewById(R.id.ptr_scrollview);

            // Now get the PullToRefresh attacher from the Activity. An exercise to the reader
            // is to create an implicit interface instead of casting to the concrete Activity
            mPullToRefreshAttacher = ((FragmentTabsActivity) getActivity())
                    .getPullToRefreshAttacher();

            // Now set the ScrollView as the refreshable view, and the refresh listener (this)
            mPullToRefreshAttacher.setRefreshableView(scrollView, this);

            // Set title in Fragment for display purposes.
            TextView title = (TextView) view.findViewById(R.id.tv_title);
            title.setText(mTitle);

            return view;
        }

问题是在我的 ListFragment 类中,我只有 onCreate 方法。

我可以使用什么来集成 onCreateView 方法?

这是我班级的完整代码:

package com.rss.alsace;

import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher;

import com.rss.utils.WebBrowserViewActivity;

import android.app.ListFragment;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;


public class AlsaceNewsFragment extends ListFragment implements PullToRefreshAttacher.OnRefreshListener {

    private static final String STATE_ACTIVATED_POSITION = "activated_position";
    //private List<String> item = new ArrayList<String>();
    private int mActivatedPosition = ListView.INVALID_POSITION;
    private RssServiceAlsace rssService;
    private static final String URL_LALSACE = "http://www.lalsace.fr/actualite/alsace/rss";

    private PullToRefreshAttacher mPullToRefreshAttacher;

    public AlsaceNewsFragment() {
        // Empty constructor required for fragment subclasses
    }

    // Allow Activity to pass us it's PullToRefreshAttacher
    void setPullToRefreshAttacher(PullToRefreshAttacher attacher) {
        mPullToRefreshAttacher = attacher;
    }

    public void onRefreshStarted(View view) {
        /**
         * Simulate Refresh with 4 seconds sleep
         */
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                refreshList();
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);

                // Notify PullToRefreshAttacher that the refresh has finished
                mPullToRefreshAttacher.setRefreshComplete();
            }
        }.execute();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        refreshList();
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
            setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
        }
    }

    public void setActivatedPosition(int position) {
        if (position == ListView.INVALID_POSITION) {
            getListView().setItemChecked(mActivatedPosition, false);
        } else {
            getListView().setItemChecked(position, true);
        }
        mActivatedPosition = position;
    }

    private void refreshList(){
        rssService = new RssServiceAlsace(this);
        rssService.execute(URL_LALSACE);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Do something with the data
        Log.d("DernieresNewsFragment", "Item cliqué ! Position=" +position);
        Intent intent = new Intent(getActivity(), WebBrowserViewActivity.class);
        ArticleAlsace article = (ArticleAlsace) this.getListAdapter().getItem(position);
        Log.d("DernieresNewsFragment", "Url chargée=" +article.getUrl());
        intent.putExtra("URL", article.getUrl());
        startActivity(intent);
    }

}
4

1 回答 1

1

onCreateView()在示例应用程序中使用,因为它正在使用片段。您不必在 Activity 中使用它。请参阅自述文件的使用部分- 它显示了如何在onCreate()活动中使用它。

于 2013-07-03T12:17:54.970 回答