17

当我关闭屏幕并重新打开时,我的应用程序将关闭。

Logcat 告诉我这个错误的原因是指向我的 CourseFragment.class 中的 java line 60,它扩展了 Fragment。

mViewPager.setAdapter(infoTechPageAdapter);

我在我的 CourseFragment.class 中实现了一个 ViewPager,它扩展了 Fragment。

这是我的代码:

public class CourseFragment extends Fragment {

public static final String ARG_POSITION_NUMBER = "course_number";

private int position;

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView;
    position = getArguments().getInt("position");

    String course = getResources().getStringArray(
            R.array.array_navigation_drawer)[position];

    getActivity().setTitle(course);

    rootView = inflater.inflate(R.layout.activity_drawer_fragment,
            container, false);

    return rootView;
}// end onCreateView

@Override
public void onStart() {
    super.onStart();

    ViewPager mViewPager = (ViewPager) getActivity().findViewById(
            R.id.pagerYear);

    Log.i("posit", String.valueOf(position));
    switch (position) {

    case 0:
        InfoTechPageAdapter infoTechPageAdapter = new InfoTechPageAdapter(
                getActivity().getSupportFragmentManager());
        mViewPager.setAdapter(infoTechPageAdapter);
        break;

    case 1:
        ComSciPageAdapter comSciPageAdapter = new ComSciPageAdapter(
                getActivity().getSupportFragmentManager());
        mViewPager.setAdapter(comSciPageAdapter);
        break;

    case 2:
        ProfilePageAdapter profilePageAdapter = new ProfilePageAdapter(
                getActivity().getSupportFragmentManager());
        mViewPager.setAdapter(profilePageAdapter);
        break;
    }

}// end onStart
}

日志猫

09-22 10:54:03.830: E/AndroidRuntime(2917): FATAL EXCEPTION: main
09-22 10:54:03.830: E/AndroidRuntime(2917): java.lang.IllegalStateException: Observer android.support.v4.view.ViewPager$PagerObserver@4239b3e8 was not registered.
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.database.Observable.unregisterObserver(Observable.java:69)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.view.PagerAdapter.unregisterDataSetObserver(PagerAdapter.java:294)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at com.usjr.sss.adapter.InfoTechPageAdapter.unregisterDataSetObserver(InfoTechPageAdapter.java:21)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:409)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at com.usjr.sss.fragment.CourseFragment.onStart(CourseFragment.java:60)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.Fragment.performStart(Fragment.java:1502)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:957)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:415)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at com.usjr.sss.fragment.CourseFragment.onStart(CourseFragment.java:60)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.Fragment.performStart(Fragment.java:1502)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:957)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1882)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:573)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.app.Activity.performStart(Activity.java:5114)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.app.Activity.performRestart(Activity.java:5169)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.app.ActivityThread.handleSleeping(ActivityThread.java:3408)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.app.ActivityThread.access$2700(ActivityThread.java:153)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1426)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.os.Looper.loop(Looper.java:137)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.app.ActivityThread.main(ActivityThread.java:5227)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at java.lang.reflect.Method.invokeNative(Native Method)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at java.lang.reflect.Method.invoke(Method.java:511)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

43

正如@Luksprog 在评论中指出的那样,您需要更改

getActivity().getSupportFragmentManager()

为了

getChildFragmentManager()

为什么:(getSupportFragmentManager()getFragmentManager())用于与与此片段的活动关联的片段进行交互,这不是您想要的。

您想要的是在此 Fragment 内部放置和管理 Fragment(使用您的适配器),这是getChildFragmentManager()方法的描述。

非常感谢@Luksprog 的指点;我遇到了和你一样的问题,他有答案!只需将他的答案发布在一个真实的答案中,对于那些可能不认为在评论中寻找这个问题的答案的人。

于 2013-11-28T18:52:01.000 回答
3

即使我100%确定我使用的是正确的FragmentManager. 我通过在初始化ViewPager's adapter.

if(pager.getAdapter() == null)
    pager.setAdapter(pagerAdapter);

在研究了堆栈跟踪之后,我认为问题在于ViewPager在释放旧适配器时试图破坏旧缓存的片段并且出现问题,因为这些片段实际上从未可见。这只是一个猜测。

于 2014-07-15T20:38:44.520 回答
0

我在使用 Android Studio 生成的代码时遇到了这个问题,并在page.getAdapter() == null每次创建之前使用了上面的检查onStart()

最终代码:

@Override
public void onStart() {
    super.onStart();

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    if(mViewPager.getAdapter() == null) {
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }
}

我现在唯一的问题是需要按两次后退按钮才能正常工作。不确定这是 Intent 还是 Fragment 问题...

于 2014-11-18T12:07:07.350 回答