2012-08-31 46 views
6

我收到一個NullPointerException當我離開FragmentActivity並轉到ListActivity。實際上,在我接近警隊之前,我開始看清名單。但是LogCat指的是我剛剛來的Activity。onSaveInstanceState與AndroidFragments的NullPointerException

的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) 

119線在MasterCat是:

@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); 
    } 
} 
+0

把你的活動和片段代碼也 – waqaslam

+0

@Waqas每個請求只增加了很多代碼。 – KickingLettuce

+0

在發佈之前是否存在以前的錯誤消息? – Proxy32

回答

13

它看起來這是一個known bug in the Android Support Library。幸運的是,它已被修補。

修復bug:

  1. 使用SDK管理器更新到支持庫的最新版本
  2. 替換android-support-v4.jar文件中的應用程序的libs/文件夾中的新版本,您剛剛下載
2

試試這個:

public void onSaveInstanceState(Bundle savedInstanceState) { 

if (savedInstanceState != null) 
    { 
     super.onSaveInstanceState(savedInstanceState); 
    } 
} 
+1

這沒有奏效。它仍然正好超過條件,就好像它不是空的,並在同一行「super.onSave ...」上跳轉。有人說這可能是支持庫的一個bug。所以我現在正在努力解決這個問題,作爲我的下一個選擇。 – KickingLettuce

+0

好吧。 :)讓我們知道,如果修復工作。 – rsb2097

+2

嗯,我只是更新了支持庫 - 它有一個錯誤。一切都很好。 – KickingLettuce

0

如果你只是想快速解決方法,添加到您的片段代碼:

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

    super.onSaveInstanceState(outState); 
} 

setUserVisibleHint屬性確實只是一個優化提示,不應該給您帶來麻煩,請參閱Android reference documentation

當然,從長遠來看,支持庫更新爲answered by theisenp可能更爲理想。