简体   繁体   English

屏幕关闭时,OnPause返回NullPointerException

[英]OnPause return NullPointerException when screen is turned off

I have a TextView correctly initialized in my Activity . 我在我的Activity正确初始化了TextView I can use it in the code without any problems. 我可以在代码中使用它而没有任何问题。 In the onPause() method, I use myTextView.setText(""); onPause()方法中,我使用myTextView.setText(""); . It works fine if I press home button or I start a new Activity , but when I use the power button to power off the screen I get a NullPointerException thrown by the setText() line. 如果我按下主页按钮或我开始一个新的Activity它工作正常,但当我使用电源按钮关闭屏幕时,我得到一个由setText()行抛出的NullPointerException

I guess that my class members are reset to null by the power off event before onPause() is called. 我想在调用onPause()之前,我的类成员被断电事件重置为null。 Which is the correct way to handle this case? 处理这种情况的正确方法是什么?

Should I catch the broadcast SCREEN_OFF or is there something wrong in my code? 我应该抓住广播SCREEN_OFF还是我的代码有问题?

My code is as simple as 我的代码很简单

@Override
protected void onPause(){
    myTextView.setText("");
    super.onPause();
}

Logcat output Logcat输出

03-04 19:58:44.783: E/AndroidRuntime(6717): FATAL EXCEPTION: main
03-04 19:58:44.783: E/AndroidRuntime(6717): Process: com.myapp, PID: 6717
03-04 19:58:44.783: E/AndroidRuntime(6717): java.lang.RuntimeException: Unable to pause activity {com.myapp/com.myapp.MyActivity}: java.lang.NullPointerException
03-04 19:58:44.783: E/AndroidRuntime(6717):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2307)
03-04 19:58:44.783: E/AndroidRuntime(6717):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3758)
03-04 19:58:44.783: E/AndroidRuntime(6717):     at android.app.ActivityThread.access$900(ActivityThread.java:145)
03-04 19:58:44.783: E/AndroidRuntime(6717):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
03-04 19:58:44.783: E/AndroidRuntime(6717):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-04 19:58:44.783: E/AndroidRuntime(6717):     at android.os.Looper.loop(Looper.java:136)
03-04 19:58:44.783: E/AndroidRuntime(6717):     at android.app.ActivityThread.main(ActivityThread.java:5081)
03-04 19:58:44.783: E/AndroidRuntime(6717):     at java.lang.reflect.Method.invokeNative(Native Method)
03-04 19:58:44.783: E/AndroidRuntime(6717):     at java.lang.reflect.Method.invoke(Method.java:515)
03-04 19:58:44.783: E/AndroidRuntime(6717):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
03-04 19:58:44.783: E/AndroidRuntime(6717):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-04 19:58:44.783: E/AndroidRuntime(6717):     at dalvik.system.NativeStart.main(Native Method)
03-04 19:58:44.783: E/AndroidRuntime(6717): Caused by: java.lang.NullPointerException
03-04 19:58:44.783: E/AndroidRuntime(6717):     at com.myapp.MyActivity.onPause(MyActivity.java:35)
03-04 19:58:44.783: E/AndroidRuntime(6717):     at android.app.Activity.performPause(Activity.java:5335)
03-04 19:58:44.783: E/AndroidRuntime(6717):     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1233)
03-04 19:58:44.783: E/AndroidRuntime(6717):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2285)
03-04 19:58:44.783: E/AndroidRuntime(6717):     ... 11 more

And here is how I initialize my TextView 以下是我初始化TextView

private TextView myTextView;
@Override
public void onResume() {
    super.onResume();
    //new AsyncTask
    //onPostExecute(){
    myTextView = (TextView) findViewById(R.id.textView1);
    //}}.execute();
}

I know it is initialized inside an AsyncTask , but I can definitely use it during Activity lifecycle. 我知道它是在AsyncTask初始化的,但我肯定可以在Activity生命周期中使用它。 I have some Button that if clicked change its text and they work, also onPause() works. 我有一些Button ,如果点击更改其文本,他们工作,也onPause()工作。 This only happens when screen is turned off... 这仅在屏幕关闭时发生...

UPDATE: For some weird reason when I press the power button my Activity performs onPause() as expected, then onResume() and onPause() an instant later. 更新:出于一些奇怪的原因,当我按下电源按钮时,我的Activity按预期执行onPause() ,然后稍后执行onResume()onPause() What can cause this? 是什么导致这个?

I suspect the issue here is that you didn't call super.onPause() first in the method and got a SuperNotCalledException . 我怀疑这里的问题是你没有在方法中首先调用super.onPause()并得到一个SuperNotCalledException

You want something like this: 你想要这样的东西:

@Override
protected void onPause() {
    super.onPause();
    myTextView.setText("");
}

I found the answer to my question in the answer to [this][1] question. 我在[this] [1]问题的答案中找到了我的问题的答案。 The screen lock was recreating my Activity in portrait mode, because it's Android default behavior after screen is locked. 屏幕锁定正在以纵向模式重新创建我的Activity ,因为它是屏幕锁定后的Android默认行为。 Calling onResume and onPause right after, caused the NullPointerException because the AsyncTask had not finished doing his work. onResume调用onResumeonPause导致NullPointerException因为AsyncTask还没有完成他的工作。

So I solved this issue by simply checking that the values are not null (not yet initialized) 所以我通过简单地检查值是否为空(尚未初始化)来解决这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM