如果使用具有自定义视图的操作栏,则不会在配置更改时调用活动onCreate

时间:2014-06-12 14:40:33

标签: android android-actionbar

我在我正在使用的操作栏中添加了自定义视图。一切都很好,直到设备旋转。未调用活动onCreate,随后在需要操作栏自定义视图的片段中存在NPE。

OTOH,如果我在活动中注释掉所有与操作栏相关的代码。一切都很好,但没有自定义操作栏。活动的基础是:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  // LINE 51
    Crashlytics.start(this);
    Log.d(TAG, "onCreate");
    setContentView(R.layout.homescreen_activity);

    actionBarCustomView = LayoutInflater.from(this).inflate(R.layout.homescreen_actionbar, null);
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
            ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(actionBarCustomView, params);

    Log.d(TAG, "Activity actionbar NULL ? " + (null == getSupportActionBar()));
    Log.d(TAG, "Activity customview NULL ? " + (null == actionBarCustomView));

    settingsFromActionbar = (ImageButton) actionBarCustomView.findViewById(R.id.settingsFromActionBar);
    settingsFromActionbar.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(HomeScreenActivity.this, "Touched Settings", Toast.LENGTH_SHORT).show();
            Intent settingIntent = new Intent(HomeScreenActivity.this, SettingsActivity.class);
            startActivity(settingIntent);

        }
    });

    fm = getSupportFragmentManager();
    homeScreenFragment = fm.findFragmentByTag("homeScreen");

    if (homeScreenFragment == null) {
        homeScreenFragment = new HomeScreenFragment();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.homescreen_container, homeScreenFragment, "homeScreen").commit();
    }
}

}
编辑:我的mainfest文件中没有android:configChanges -

<!-- HOME SCREEN ACTIVITY -->
    <activity
        android:name="com.example.try_masterdetail.homescreen.HomeScreenActivity"
        android:label="@string/app_name"
        android:theme="@style/TrasnparentActionBarTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

编辑:LOGCAT -

  FATAL EXCEPTION: main


Process: com.example.try_masterdetail, PID: 5240
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.try_masterdetail/com.example.try_masterdetail.homescreen.HomeScreenActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
    at android.app.ActivityThread.access$900(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
  Caused by: java.lang.NullPointerException
    at com.example.try_masterdetail.homescreen.HomeScreenFragment.onAttach(HomeScreenFragment.java:94)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1097)
    at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1890)
    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:215)
    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:97)
    at com.example.try_masterdetail.homescreen.HomeScreenActivity.onCreate(HomeScreenActivity.java:51)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    ... 12 more

HomeScreenFragment -

    @Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        activityCallback = (Callbacks) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement ToolbarListener");
    }

    Log.d(TAG, "fragment activity NULL ? " + (null == activity));  // This comes as false
    actionBar = ((ActionBarActivity) activity).getSupportActionBar();
    Log.d(TAG, "fragment actionbar NULL ? " + (null == actionBar)); // This comes as true
    actionBarCustomView = actionBar.getCustomView(); //LINE 94

}

1 个答案:

答案 0 :(得分:0)

显然,当旋转设备时,在Activity的onCreate()之前调用片段的onAttach()。因此当程序试图在Fragment的onAttach()中获取它时,动作条从未形成。

更多 - After the rotate, onCreate() Fragment is called before onCreate() FragmentActivity