在Activity上调用onSaveInstanceState

时间:2018-10-10 22:47:49

标签: android

我继承了一个项目,试图理解并解决一个旧错误。

当我开始一项活动并将其搁置一会儿(有时几乎是即时的)时,但它似乎进入了暂停状态或某种状态,并尝试重新加载该活动并收到错误消息。

“由于:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String ...'”

当我将其添加到活动中时,我发现:

@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
  //super.onSaveInstanceState(savedInstanceState);
}

它看起来运行良好。

但是,当我取消注释上面的行到super.onSaveInstanceState(savedInstanceState);时,我再次收到错误消息。

如果有帮助,我的活动就是扩展ActionBarActivity。

任何帮助将不胜感激。谢谢!

相关代码:

@Override
protected void prepareLayout() {
  textMessage = (TextView) findViewById(R.id.textMessage);
  textNotes = (EditText) findViewById(R.id.textNotes);

  textMessage.setText(getOneOfMyItems().getText());
  // ... the above line is where the problem error is thrown.
}

Stack Trace:
    Process: com.myproj.myproj, PID: 9272
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproj.myproj/com.myproj.activities.MyActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.myproj.data.MyItems.getText()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3288)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3384)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5433)
        at android.app.ActivityThread.access$1200(ActivityThread.java:226)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1810)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7270)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.myproj.data.MyItems.getText()' on a null object reference
        at com.myproj.xyz.MyFragment.prepareLayout(MyFragment.java:44)
        at com.myproj.fragments.BaseFragment.prepareAndSetView(BaseFragment.java:49)
        at         at com.myproj.xyz.BaseMyFragment.onCreateView(BaseMyFragment.java:21)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2143)
        at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2089)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1129)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2143)
        at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:615)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1266)
        at android.app.Activity.performStart(Activity.java:6943)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3384) 
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5433) 
        at android.app.ActivityThread.access$1200(ActivityThread.java:226) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1810) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:7270) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

0 个答案:

没有答案