值未保存在savedInstanceState中

时间:2012-12-23 04:30:42

标签: android gridview logcat instances

由于某种原因,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()) );

}

}

logcat

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

4 个答案:

答案 0 :(得分:0)

Bundle::get系列函数将标记 id 作为参数。这就是他们如何知道你究竟要求的东西。即,

// get a String with id "R1C1", note the quotes
R1C1 = savedInstanceState.getString("R1C1");

你打电话给他们的方式,因为你正在处理Strings,这只是偶然的机会,实际上是要求String id 的任何R1C1 1}}当时包含。

但是你将ints放在捆绑包上并期望提取Strings,这是一个简单而简单的错误。

答案 1 :(得分:0)

您的清单中如何声明父Activity?如果您正在处理配置更改,即

android:configChanges= whatever

您的Activity,因此您的Fragment将无法重新创建。

答案 2 :(得分:0)

你可能是自动(非)拳击的另一个受害者。我怀疑,因为Bundle.putInt()int作为第二个参数,而您为其提供的值Integer.valueOf()会返回Integer。如果返回的Integer不是null,则会很好,但是取消装箱将无法将null转换为普通int并导致NPE。

做什么是特定于案例的,取决于您的应用程序的功能。无论如何,请确保Integer.valueOf()不返回null

答案 3 :(得分:0)

如果您正在设置

setRetainInstance(true) 
在片段中

或从活动中设置它:

myFragment.setRetainInstance(true):

然后,从onSaveInstanceState()传递到onCreate()并且onCreateView()的Bundle将为空,即使您在onSaveInstanceState()中添加了额外内容。

请参阅此答案:how i can break things with Fragments with setRetainInstance(true) and adding them to backstack?