2012-02-05 76 views
2

如果我背景的應用程序,並在一段時間後恢復它,我收到以下異常。
如果我改變方向或背景並立即恢復應用程序(在兩種情況下都會執行onSaveInstanceStateonCreate),那麼savedInstanceState Bundle包含正確的ArrayList,並且一切正常。FragmentActivity NullPointer onCreate savedInstanceState Bundle

02-05 14:42:06.254 E/AndroidRuntime(24355): 了java.lang.RuntimeException:無法啓動活動 ComponentInfo {com.savant.donordetailsviewpagertitle/com.savant.donordetailsviewpagertitle.activities.DonorDetailsContainerFragmentActivity }: 顯示java.lang.NullPointerException:預計類型 com.savant.donordetailsviewpagertitle.activities.DonorDetailsContainerFragmentActivity $ RunningLoadersList, 的接收者,而是得到了空

private class RunningLoadersList extends ArrayList<RunningLoader> implements 
     Parcelable { 

    private static final long serialVersionUID = 663585476779879096L; 

    public RunningLoadersList() { 
    } 

    @SuppressWarnings("unused") 
    public RunningLoadersList(Parcel in) { 
     this(); 
     readFromParcel(in); 
    } 

    private void readFromParcel(Parcel in) { 

     this.clear(); 

     // First we have to read the list size 
     int size = in.readInt(); 

     for (int i = 0; i < size; i++) { 
      RunningLoader r = new RunningLoader(in.readInt(), 
        in.readBundle()); 
      this.add(r); 
     } 
    } 

    public int describeContents() { 
     return 0; 
    } 

    public final Parcelable.Creator<RunningLoadersList> CREATOR = new Parcelable.Creator<RunningLoadersList>() { 
                    public RunningLoadersList createFromParcel(Parcel in) { 
                     return new RunningLoadersList(in); 
                    } 

                    public RunningLoadersList[] newArray(int size) { 
                     return new RunningLoadersList[size]; 
                    } 
                   }; 

    public void writeToParcel(Parcel dest, int flags) { 
     int size = this.size(); 

     // We have to write the list size, we need him recreating the list 
     dest.writeInt(size); 

     for (int i = 0; i < size; i++) { 
      RunningLoader r = this.get(i); 

      dest.writeInt(r.id); 
      dest.writeBundle(r.args); 
     } 
    } 
} 

static final class RunningLoader { 
    private final int  id; 
    private final Bundle args; 

    RunningLoader(int _id, Bundle _args) { 
     id = _id; 
     args = _args; 
    } 
} 



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

    // This has to be called before setContentView and you must use the 
    // class in android.support.v4.view and NOT android.view 
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 

    setContentView(R.layout.main); 

    Log.d(LOG_TAG, "onCreate"); 

    mAdapter = new ViewPagerTitleAdapter(getSupportFragmentManager()); 

    mPager = (ViewPager) findViewById(R.id.pager); 
    mPager.setAdapter(mAdapter); 

    TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.tabindicator); 
    indicator.setViewPager(mPager); 

    // first check if we already have a running loader 
    if ((savedInstanceState != null) 
      && savedInstanceState.containsKey("RUNNING_LOADERS")) { 
     mRunningLoaders = savedInstanceState.getParcelable("RUNNING_LOADERS"); 
    } 

    if (mRunningLoaders == null) { 
     mRunningLoaders = new RunningLoadersList(); 
    } 

    if (mRunningLoaders != null) { 
     for (int i = 0; i < mRunningLoaders.size(); i++) { 
      StartLoader(mRunningLoaders.get(i).id, 
        mRunningLoaders.get(i).args); 
     } 
    } 

    if (getSupportLoaderManager().hasRunningLoaders()) { 
     setProgressBarIndeterminateVisibility(Boolean.TRUE); 
    } else { 
     setProgressBarIndeterminateVisibility(Boolean.FALSE); 
    } 
} 


    @Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putParcelable("RUNNING_LOADERS", mRunningLoaders); 

    Log.d(LOG_TAG, "onSaveInstanceState"); 
} 
+0

只是堅持圍繞mRunningLoaders = savedInstanceState.getParcelable一個try/catch( 「RUNNING_LOADERS」); 不理想,我想知道爲什麼savedInstanceState返回null時,我已經做了檢查!= null ... – MartinS 2012-02-07 09:00:20

+0

嗯。嘗試/ catch不阻止異常並強制關閉。有任何想法嗎 ? – MartinS 2012-02-07 19:06:13

回答

11

Parcelable.Creator<RunningLoadersList>()必須static這樣

public static final Parcelable.Creator<RunningLoadersList> CREATOR = 
new Parcelable.Creator<RunningLoadersList>() { 
... 
... 
. 
} 
相關問題