0

希望这是我关于 Fragments 的最后一期 :)

我有两个片段(一个常规另一个列表),当我在 main_activity 上运行 setContentView 时,我注意到正在调用常规片段的 onCreate,另一方面,没有调用 ListFragment onCreate。

除了我没有在 ListFragment 中覆盖 onAttach + onDetach 之外,它们几乎相同(它是为常规回调中的回调完成的)。我所做的覆盖是:onCreate、onCreateView、onActivityCreated、onStart(启动异步任务)、onSaveInstanceState。

你能告诉我问题是什么吗?因为我在 ListFragment 的 FragmentManager 中获得了 NullPointerEception。

一些代码:

活动主.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mynews.mynews.MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.mynews.mynews.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />

    <fragment android:id="@+id/rss_feed"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:name="com.kfir.bs.mynews.mynews.NewsFeed"
        tools:layout="@layout/fragment_news_feed" />

</android.support.v4.widget.DrawerLayout>

关于活动:

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            setContentView(R.layout.activity_main);
            mNavigationDrawerFragment = (NavigationDrawerFragment)
                    getFragmentManager().findFragmentById(R.id.navigation_drawer);
            mNewsFeed = (NewsFeed) getFragmentManager().findFragmentById(R.id.rss_feed);

在片段上:

public class NavigationDrawerFragment extends Fragment {
    private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
    private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
    private NavigationDrawerCallbacks mCallbacks;
    private ActionBarDrawerToggle mDrawerToggle;

    private DrawerLayout mDrawerLayout;
    private ListAdapter adapter;
    private ListView mDrawerListView;
    private View mFragmentContainerView;

    private int mCurrentSelectedPosition = 0;
    private boolean mFromSavedInstanceState;
    private boolean mUserLearnedDrawer;

    public NavigationDrawerFragment() {
    }

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

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
        mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);

        if (savedInstanceState != null) {
            mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
            mFromSavedInstanceState = true;
        }

        adapter = new ListAdapter(getActionBar().getThemedContext(), generateData());

        selectItem(mCurrentSelectedPosition);
    }

在 ListFragment 上:

public class NewsFeed extends ListFragment
{
    private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";

    View feedView;
    ListAdapterFeed rssListAdapter;

    private int mCurrentSelectedPosition = 0;
    private boolean mFromSavedInstanceState;

    public NewsFeed() {
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

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

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());

        if (savedInstanceState != null) {
            mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
            mFromSavedInstanceState = true;
        }

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

添加堆栈跟踪:

https://www.dropbox.com/s/dxrz0925u3nfmgp/ddms7983186720865326533.trace

日志猫:

mynews D/dalvikvm: +++ 活动分析器计数现在 1 04-19 23:16:24.391 11473-11473/com.kfir.bs.mynews.mynews E/MoreInfoHPW_ViewGroup: 父视图不是 TextView 04-19 23:16 :27.771 11473-12572/com.kfir.bs.mynews.mynews I/dalvikvm:threadid=11:等待方法跟踪完成 04-19 23:16:48.961 11473-11473/com.kfir.bs.mynews.mynews E/MyNews: Exceptionjava.lang.NullPointerException 04-19 23:17:15.451 11473-11479/com.kfir.bs.mynews.mynews D/dalvikvm: +++ active profiler count now 0 04-19 23:17:15.711 11473-11479/com.kfir.bs.mynews.mynews I/dalvikvm﹕跟踪已停止:写入 44458 条记录 04-19 23:17:15.711 11473-11479/com.kfir.bs.mynews.mynews W/dalvikvm﹕警告:调试器处于活动状态;方法跟踪结果将出现偏差 mynews E/MoreInfoHPW_ViewGroup﹕父视图不是 TextView 04-19 23:16:27.771 11473-12572/com.kfir.bs.mynews.mynews I/dalvikvm﹕threadid=11: 等待方法跟踪完成 04-19 23 :16:48.961 11473-11473/com.kfir.bs.mynews.mynews E/MyNews: Exceptionjava.lang.NullPointerException 04-19 23:17:15.451 11473-11479/com.kfir.bs.mynews.mynews D/dalvikvm ﹕ +++ 活动探查器计数现在 0 04-19 23:17:15.711 11473-11479/com.kfir.bs.mynews.mynews I/dalvikvm﹕跟踪停止:写入 44458 条记录 04-19 23:17:15.711 11473- 11479/com.kfir.bs.mynews.mynews W/dalvikvm:警告:调试器处于活动状态;方法跟踪结果将出现偏差 mynews E/MoreInfoHPW_ViewGroup﹕父视图不是 TextView 04-19 23:16:27.771 11473-12572/com.kfir.bs.mynews.mynews I/dalvikvm﹕threadid=11: 等待方法跟踪完成 04-19 23 :16:48.961 11473-11473/com.kfir.bs.mynews.mynews E/MyNews: Exceptionjava.lang.NullPointerException 04-19 23:17:15.451 11473-11479/com.kfir.bs.mynews.mynews D/dalvikvm ﹕ +++ 活动探查器计数现在 0 04-19 23:17:15.711 11473-11479/com.kfir.bs.mynews.mynews I/dalvikvm﹕跟踪停止:写入 44458 条记录 04-19 23:17:15.711 11473- 11479/com.kfir.bs.mynews.mynews W/dalvikvm:警告:调试器处于活动状态;方法跟踪结果将出现偏差 kfir.bs.mynews.mynews E/MyNews: Exceptionjava.lang.NullPointerException 04-19 23:17:15.451 11473-11479/com.kfir.bs.mynews.mynews D/dalvikvm: +++ active profiler count now 0 04 -19 23:17:15.711 11473-11479/com.kfir.bs.mynews.mynews I/dalvikvm﹕跟踪已停止:写入 44458 条记录 04-19 23:17:15.711 11473-11479/com.kfir.bs.mynews。 mynews W/dalvikvm:警告:调试器处于活动状态;方法跟踪结果将出现偏差 kfir.bs.mynews.mynews E/MyNews: Exceptionjava.lang.NullPointerException 04-19 23:17:15.451 11473-11479/com.kfir.bs.mynews.mynews D/dalvikvm: +++ active profiler count now 0 04 -19 23:17:15.711 11473-11479/com.kfir.bs.mynews.mynews I/dalvikvm﹕跟踪已停止:写入 44458 条记录 04-19 23:17:15.711 11473-11479/com.kfir.bs.mynews。 mynews W/dalvikvm:警告:调试器处于活动状态;方法跟踪结果将出现偏差

在此先感谢,

4

0 回答 0