score:70

Accepted answer

I'm almost sure that this is caused when the thread finish its work but the activity is no longer visible.

You should check if the getActivity() call return null, and ...

To apply corrections on your code, look at this:

// (Calendar) Date function - Displays dateview on Card
final boolean keepRunning1 = true;
Thread thread_two = new Thread(){

@Override
public void run(){

    while(keepRunning1){

        // Make the thread wait half a second. If you want...
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            Toast.makeText(getActivity().getApplicationContext(), "Default Signature                         Fail", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

        // here you check the value of getActivity() and break up if needed
        if(getActivity() == null)
            return;

        getActivity().runOnUiThread(new Runnable(){
        @Override
        public void run(){
           TextView date = (TextView) getView().findViewById(R.id.date);
           date.setText(DateUtils.formatDateTime(getActivity().getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR));
           }
         });
    }
}
};thread_two.start();

score:5

Try this one

TextView date = (TextView) getView().findViewById(R.id.date);

is date is null or not check

if(date !=null){
                    date.setText(DateUtils.formatDateTime(getActivity().getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR));
}

score:8

The reason for the NPE is that your thread is not bound to the fragment lifecycle. Once the fragment is detached from its hosting activity, getActivity() returns null.

As a solution, consider removing the thread altogether and just use postDelayed() on a Handler on the UI thread to post Runnables that do the updates you want after a delay.

score:10

Put

if(getActivity() == null)
        return;

before getActivity().runOnUiThread(new Runnable(){ that way when the back button is closed and your Thread is still running it will check whether the calling Activity still exists.

If it does not it will return.

score:21

After pressing back, your background thread is still running. By the time that thread reaches the getActivity().runOnUiThread() code, the activity no longer exists. Check if the activity still exists like so:

if (getActivity() != null) {
        getActivity().runOnUiThread(new Runnable(){
                @Override
                public void run(){
                    TextView date = (TextView) getView().findViewById(R.id.date);
                    date.setText(DateUtils.formatDateTime(getActivity().getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR));
            }
        });
}

Related Query

More Query from same tag