6

当我离开 FragmentActivity 并转到 ListActivity 时,我收到 NullPointerException。在我得到强制关闭之前,我实际上开始看到列表。但是 LogCat 指的是我刚刚来自的 Activity。

LogCat 是这样说的:

08-31 07:38:53.356: E/AndroidRuntime(8134): FATAL EXCEPTION: main
08-31 07:38:53.356: E/AndroidRuntime(8134): java.lang.NullPointerException
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at com.---.myApp.MasterCat.onSaveInstanceState(MasterCat.java:119)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.app.Activity.performSaveInstanceState(Activity.java:1137)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1215)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3077)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3136)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.app.ActivityThread.access$900(ActivityThread.java:142)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1235)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.os.Looper.loop(Looper.java:137)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.app.ActivityThread.main(ActivityThread.java:4928)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at java.lang.reflect.Method.invokeNative(Native Method)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at java.lang.reflect.Method.invoke(Method.java:511)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at dalvik.system.NativeStart.main(Native Method)

MasterCat 中的第 119 行是:

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

我不确定要显示什么其他代码,请告诉我。我的基本设置包含 4 个静态内部类,它们都是父级内部的所有片段FragmentActivity(使用支持库)。它使用标签和viewpager。

编辑,更多代码:

public class MasterCat extends FragmentActivity implements
        ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_layout);
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager
                .setOnPageChangeListener(
                    new ViewPager.SimpleOnPageChangeListener() {

                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }
    }

    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {}

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

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

    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {}

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment f = null;
            switch (position) {
            case 0:
            {
                f = new MasterFrag();
                Bundle args = new Bundle();
                f.setArguments(args);
                break;
            }
            case 1:
            {
                f = new FeaturedFrag();
                Bundle args = new Bundle();
                f.setArguments(args);
                break;
            }
            case 2:
            {
                f = new TopFrag();
                Bundle args = new Bundle();
                f.setArguments(args);
                break;
            }
            case 3:
            {
                f = new NewFrag();
                Bundle args = new Bundle();
                f.setArguments(args);
                break;
            }
            default:
                throw new IllegalArgumentException("not this many fragments: "
                    + position);
            }
            return f;
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return getString(R.string.mastercattab1).toUpperCase();
            case 1:
                return getString(R.string.mastercattab2).toUpperCase();
            case 2:
                return getString(R.string.mastercattab3).toUpperCase();
            case 3:
                return getString(R.string.mastercattab4).toUpperCase();
            }
            return null;
        }
    }
}

一个典型的片段有这样的轮廓:

public static class FeaturedFrag extends Fragment {

    public FeaturedFrag() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.rateithome, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}
4

4 回答 4

13

看起来这是Android 支持库中的一个已知错误。幸运的是,它已被修补。

要修复错误:

  1. 使用 SDK Manager 更新到最新版本的支持库
  2. android-support-v4.jar将应用程序文件夹中的文件替换为libs/您刚刚下载的新版本
于 2012-08-31T16:53:06.280 回答
2

尝试这个:

public void onSaveInstanceState(Bundle savedInstanceState) {

if (savedInstanceState != null) 
   {
      super.onSaveInstanceState(savedInstanceState);
    }
 }
于 2012-08-31T16:01:54.127 回答
0

如果您只是想要一个快速的解决方法,请将其添加到您的片段代码中:

@Override
public void onSaveInstanceState(Bundle outState) {
    // Workaround to avoid NPE from support library bug:
    setUserVisibleHint(false);

    super.onSaveInstanceState(outState);
}

setUserVisibleHint属性确实只是优化的提示,不会给您带来任何麻烦,请参阅Android 参考文档

当然,从长远来看,更新由 theisenp 回答的支持库可能更可取。

于 2013-07-29T12:29:00.070 回答
0

我想你用过这种风格 AppTheme.NoActionBar

在清单文件和 java 类中,您正在使用操作栏,这就是它导致崩溃的原因。

你能告诉我你的清单文件和style.xml吗

于 2018-07-09T06:52:56.753 回答