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;
}
}
我的堆栈跟踪:
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");
}
this
的弱引用?调用this.getSupportFragmentManager()
太容易了吗? - Eugen Pechanec