Wednesday 22 February 2017

java - NullPointerException on getActivity().runOnUiThread(new Runnable(){




I know there are many different causes for NPE but mine is slightly weird (At least to me).




So I have converted my Activities to Fragments successfully, but my problem appears to be coming from the function that displays the date. When the application is running, everything works just fine. But as soon as you press the back button. The app force closes, then in the log it says I'm getting NullPointerException at line 102. So looking at the code, I did research on this but unfortunately got nothing.



This is the line where the error is coming from when you press the back button.



getActivity().runOnUiThread(new Runnable(){


Also I have tried disabling the back button (As I'm building a launcher and it's not needed). But it doesn't seem to be working.




Here is the code for the whole date displaying method/function.



// (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();
}

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();



Thanks for your time, hopefully you can shed some light on what I'm doing wrong.



Logcat -



05-23 21:17:33.216: E/AndroidRuntime(6906): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.runOnUiThread(java.lang.Runnable)' on a null object reference
05-23 21:17:33.216: E/AndroidRuntime(6906): at com.activelauncher.fragments.UtilsFragment$2.run(UtilsFragment.java:102)

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();

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...