Reputation: 4920
I have a TextView
correctly initialized in my Activity
.
I can use it in the code without any problems. In the onPause()
method, I use 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.
I guess that my class members are reset to null by the power off event before onPause()
is called.
Which is the correct way to handle this case?
Should I catch the broadcast SCREEN_OFF
or is there something wrong in my code?
My code is as simple as
@Override
protected void onPause(){
myTextView.setText("");
super.onPause();
}
Logcat output
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
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. I have some Button
that if clicked change its text and they work, also onPause()
works. 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. What can cause this?
Upvotes: 0
Views: 623
Reputation: 4920
I found the answer to my question in the answer to [this][1] question.
The screen lock was recreating my Activity
in portrait mode, because it's Android default behavior after screen is locked.
Calling onResume
and onPause
right after, caused the NullPointerException
because the AsyncTask
had not finished doing his work.
So I solved this issue by simply checking that the values are not null (not yet initialized)
Upvotes: 0
Reputation: 19699
I suspect the issue here is that you didn't call super.onPause()
first in the method and got a SuperNotCalledException
.
You want something like this:
@Override
protected void onPause() {
super.onPause();
myTextView.setText("");
}
Upvotes: 2