1

出于某种原因,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
4

4 回答 4

0

函数族Bundle::get标签id作为参数。这就是他们知道你到底要什么的方式。IE,

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

你打电话给他们的方式,因为你正在处理,这只是偶然的工作Strings,实际上是要求一个String带有当时包含的任何内容的id 。R1C1

但是您正在ints打包并期望提取Strings,这是一个简单明了的错误。

于 2012-12-23T04:33:03.997 回答
0

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

android:configChanges= whatever

你的Activity,因此你的Fragment将不会被重建。

于 2012-12-23T04:54:06.050 回答
0

您可能是自动(取消)装箱的另一个受害者。我怀疑 sinceBundle.putInt()将 aint作为第二个参数,而您为它提供了一个Integer.valueOf()返回a 的值IntegerInteger如果返回的不是 is 也没关系null,但拆箱将无法转换null为普通类型int并导致 NPE。

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

于 2012-12-23T05:39:02.110 回答
0

如果你正在设置

setRetainInstance(true) 

在您的 Fragment 内部或从 Activity 设置它:

myFragment.setRetainInstance(true):

然后 Bundle 从onSaveInstanceState()to传递onCreate()并且onCreateView()将为 null,即使您在onSaveInstanceState().

看到这个答案:我如何用 setRetainInstance(true) 打破片段并将它们添加到后台堆栈?

于 2012-12-23T07:22:32.210 回答