I get a NullPointerException, which I cannot understand why this is happening. It seems that mainActivityWeakReference.get () returns null.
So, I assume that the activity is recreated, and the reference variable is set to null after the class is loaded, and the link will only be updated after the onCreate method is called. But I don’t understand that I am busy following the instructions within the framework of the activity that point to and yet the link is null.
Can activity coexist for a short time and how can I deal with this situation. It seems to me that a simple zero check will not be enough to fix the problem. Perhaps I should call finish ();
public class MainActivity extends android.support.v7.app.AppCompatActivity {
private WeakReference<AppCompatActivity> mainActivityWeakReference = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
mainActivityWeakReference = new WeakReference<AppCompatActivity>(this);
}
public void handleAction(UserAction userAction) {
switch (userAction) {
case UPDATE_UI:
<< ERROR HERE
mainActivityWeakReference.get().getSupportFragmentManager().executePendingTransactions();
break;
}
}
My stack trace:
java.lang.NullPointerException: Attempt to read from field 'android.os.Handler android.support.v4.a.m.a' on a null object reference
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1476)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
at za.co.entersekt.nedbank.MainActivity.handleAction(MainActivity.java:297)
:
public abstract class FragmentManager {
....
public boolean execPendingActions() {
if (mExecutingActions) {
throw new IllegalStateException("Recursive entry to executePendingTransactions");
}
if (Looper.myLooper() != mActivity.mHandler.getLooper()) {<<FragmentManager.java:1476
throw new IllegalStateException("Must be called from main thread of process");
}