Question

I have a problem I am using handler and runnable to update timer inside my app, inside my Runnable I am updating textview, after 1minut I want to show some content, everything works fine until I rotate the screen, every textview is now null, and I couldnt figure out why. My code:

Runnable mTimer = new Runnable() {
    @Override
    public void run() {
            textView.setText(DateFormat.format("mm:ss", timers - System.currentTimeMillis()));

        test();
            mHandler.postDelayed(this, TIME);
    }
};

Any ideas why this might happen?

Était-ce utile?

La solution

Handler probably delivers a Runnable to an Activity that was recycled. Proper use of Handler is like

private Handler mHandler;
private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mHandler = new Handler();
    setContentView(R.layout.yourView);
    mTextView = findViewById(R.id.text);
}

@Override
protected void onStart() {
    super.onStart();
    //start updating every time Activity is started
    handler.postDelayed(mTimer, oneMinuteDelay);
}

@Override
protected void onStop() {
    super.onStop();
    //make sure to remove all messages
    handler.removeCallbacksAndMessages(null);
}

Autres conseils

In theory, this (null views) should not happen.

When you change the screen orientation, the activity leaves the screen and becomes useless, but it still exists and references the views. Your runnable references the instance of activity that has created it, so the activity cannot die while the runnable is still there. At least, so it was. Which Android version do you use?

It seems I understand what you mean. You mean null contents in the views. You have to create a static variable, say, lastInstance:

class MyActivity extends Activity {
    static MyActivity lastInstance;
    void onCreate(...) {
        ...
        lastInstance = this;
    }
    // no need to reference an instance of any Activity, so static
    static class MyRunnable implements Runnable {
    @Override
    public void run() {
            lastInstance.textView.setText(DateFormat.format("mm:ss", timers - System.currentTimeMillis()));

            lastInstance.test();
            mHandler.postDelayed(this, TIME);
        }
    }

    static Runnable mTimer = new MyRunnable();
}

I do not recommend android:configChanges="screenSize|keyboardHidden|orientation" because this is not the only case when Android recreates an Activity, so this way you will not fix any bugs, you will just make them more difficult to reproduce.

For this thing you have to specify in your manifest with the specified line in your activity tag then your issue will be fixed.

i.e,

 <activity android:name="your activity" 
           android:configChanges="screenSize|keyboardHidden|orientation">

    </activity>

Then it will work for you on rotating the screen also.

Edited Answer

Better check that textview If it is null create a reference and then add the data it may fix your issue. or meanwhile you can pass your old data from onSavedInstance();

and you can get the data from onCreate(SavedInstance savedinstance)

here it will returns that prevoius data what you are setted in onsavedInstance Method.

try this for data exchange it will work

After rotate your activity recreates, so textView is null.

Please remove the handler code from the runnable. Also first create object of handler then write the handlers post delayed method where you want. Main use of handler is to update UI from thread.

If the Activity doesn't crash when you turn round the device, it means that the textView is there. If you see nulls on the screen it is the content of the textView that is being set as null. In the text, the only variable I see is timers. Where is this variable defined and where is it being set?

First check that you properly initialize the handler as below :

 handler = new Handler();

The null pointer error may come if you not initialize the handler.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top