我有BaseFragment
public abstract class BaseFragment extends Fragment {
private static String TAG = "BaseFragment";
private BaseFragmentActions baseFragmentActions;
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.i(TAG, "onAttach = ");
try {
baseFragmentActions = (BaseFragmentActions)(StartPageActivity)getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(((StartPageActivity)getActivity()).toString() + " must implement interface BaseFragmentActions");
}
baseFragmentActions.resetToolbarNavigation(getBackStackCount()!=0);//25
}
@Override
@CallSuper
public void onDetach() {
super.onDetach();
baseFragmentActions.resetToolbarNavigation((getBackStackCount() -1 )!= 0);
}
private int getBackStackCount() {
int b = getActivity().getSupportFragmentManager().getBackStackEntryCount();
return b;
}
public interface BaseFragmentActions {
public void resetToolbarNavigation(boolean backNavigationEnabled);
}
} 和片段A,B,C,它们使用BaseFragment。一切都好。后退箭头按预期工作,但如果更改方向我得到了exeption:
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: FATAL EXCEPTION: main
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.john.testOne/com.john.testOne.StartPageActivity}: java.lang.NullPointerException
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.os.Looper.loop(Looper.java:130)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:3687)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:507)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: Caused by: java.lang.NullPointerException
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at com.john.testOne.StartPageActivity.resetToolbarNavigation(StartPageActivity.java:177)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at com.john.testOne.BaseFragment.onAttach(BaseFragment.java:25)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1030)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1241)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:2048)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.support.v4.app.FragmentController.dispatchCreate(FragmentController.java:154)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:289)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:61)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at com.john.testOne.StartPageActivity.onCreate(StartPageActivity.java:34)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.os.Looper.loop(Looper.java:130)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:3687)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:507)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
如果我将onAttach更改为onActivityCreated和onDetach - 我试过onStop和onDestroy 然后不是摘要而是"回到箭头"好不好。
StartPageActivity
public class StartPageActivity extends AppCompatActivity implements BaseFragment.BaseFragmentActions {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_page_activity);;
FragmentManager fm = getSupportFragmentManager();
Fragment mFragment = new MainFragment();
fm.beginTransaction().addToBackStack(null)
.add(R.id.fragment_container,mFragment)
.commit();
initNavigation();
}
}
StartPageActivity中的resetToolbarNavigation
@Override
public void resetToolbarNavigation(boolean backNavigationEnabled) {
mActionBar.setDisplayHomeAsUpEnabled(backNavigationEnabled);//------------------177
// mActionBar.setHomeAsUpIndicator(R.drawable.ic_add);
if (backNavigationEnabled) {
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Log.i("resetToolbarNavigation", "setNavigationOnClickListener");
onBackPressed();
}
});
}
else {
initNavigation();
syncState();
}
}
如何纠正这个问题?