出于某种原因,gridView 中的值没有从 Bundle 中保存。也没有任何内容输出到 logcat。我是否正确地从 gridview 获取值?它充满了编辑文本,所以它应该从那里获取值
public class RedScorerFragment extends SherlockFragment {
LayoutInflater infl;
GridView mGrid;
int R1C1, R1C2, R1C3;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
infl = inflater;
View mView = inflater.inflate(R.layout.fragment_score_red, container, false);
mGrid = (GridView) mView.findViewById(R.id.gridViewRed);
if(savedInstanceState != null)
{
R1C1 = savedInstanceState.getInt("R1C1");
R1C2 = savedInstanceState.getInt("R1C2");
R1C3 = savedInstanceState.getInt("R1C3");
Log.d("savedd R1C1 ", String.valueOf(R1C1));
Log.d("savedd R1C2 ", String.valueOf(R1C2));
Log.d("savedd R1C3 ", String.valueOf(R1C3));
}
mGrid.setAdapter(new ImageAdapter(getActivity()));
return mView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null)
{
R1C1 = savedInstanceState.getInt("R1C1", 0);
R1C2 = savedInstanceState.getInt("R1C2", 0);
R1C3 = savedInstanceState.getInt("R1C3", 0);
Log.v("saved R1C1 ", String.valueOf(R1C1));
Log.v("saved R1C2 ", String.valueOf(R1C2));
Log.v("saved R1C3 ", String.valueOf(R1C3));
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
//for(int i=0;i<mGrid.getChildCount(); i++)
Log.e("Saving", mGrid.getChildAt(0).toString());
Log.e("Saving", mGrid.getChildAt(1).toString());
Log.e("Saving", mGrid.getChildAt(2).toString());
savedInstanceState.putInt("R1C1", Integer.valueOf(mGrid.getItemAtPosition(0).toString()) );
savedInstanceState.putInt("R1C2", Integer.valueOf(mGrid.getItemAtPosition(1).toString()) );
savedInstanceState.putInt("R1C3", Integer.valueOf(mGrid.getItemAtPosition(2).toString()) );
}
}
日志猫
12-22 23:58:38.736: E/AndroidRuntime(2481): FATAL EXCEPTION: main
12-22 23:58:38.736: E/AndroidRuntime(2481): java.lang.RuntimeException: Unable to pause activity {org.say.upscorer/org.say.upscorer.MainActivity}: java.lang.NullPointerException
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3348)
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3305)
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3288)
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.app.ActivityThread.access$2500(ActivityThread.java:125)
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2040)
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.os.Looper.loop(Looper.java:123)
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-22 23:58:38.736: E/AndroidRuntime(2481): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 23:58:38.736: E/AndroidRuntime(2481): at java.lang.reflect.Method.invoke(Method.java:521)
12-22 23:58:38.736: E/AndroidRuntime(2481): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-22 23:58:38.736: E/AndroidRuntime(2481): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-22 23:58:38.736: E/AndroidRuntime(2481): at dalvik.system.NativeStart.main(Native Method)
12-22 23:58:38.736: E/AndroidRuntime(2481): Caused by: java.lang.NullPointerException
12-22 23:58:38.736: E/AndroidRuntime(2481): at org.saywatt.scoring.RedScorerFragment.onSaveInstanceState(RedScorerFragment.java:91)
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.support.v4.app.Fragment.performSaveInstanceState(Fragment.java:1607)
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1587)
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1655)
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:527)
12-22 23:58:38.736: E/AndroidRuntime(2481): at com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:127)
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.app.Activity.performSaveInstanceState(Activity.java:1036)
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1180)
12-22 23:58:38.736: E/AndroidRuntime(2481): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3330)
12-22 23:58:38.736: E/AndroidRuntime(2481): ... 12 more