Android 如果使用带有自定义视图的actionbar,则在配置更改时不会调用活动的onCreate
我在正在使用的actionbar中添加了一个customview。在设备旋转之前,一切都很正常。不调用活动的onCreate,随后在需要actionbar的customview的片段中存在NPE OTOH,如果我注释掉活动中所有与actionbar相关的代码。一切都很好,但没有定制的actionbar。该活动的onCreate是:Android 如果使用带有自定义视图的actionbar,则在配置更改时不会调用活动的onCreate,android,android-actionbar,Android,Android Actionbar,我在正在使用的actionbar中添加了一个customview。在设备旋转之前,一切都很正常。不调用活动的onCreate,随后在需要actionbar的customview的片段中存在NPE OTOH,如果我注释掉活动中所有与actionbar相关的代码。一切都很好,但没有定制的actionbar。该活动的onCreate是: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(s
@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>
家庭破碎-
@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
}
显然,当设备旋转时,在活动onCreate之前调用片段的onAttach。因此,当程序试图在Fragment的onAttach中获取actionbar时,actionbar从未形成
更多-发布logcat错误将对我们有所帮助。帮助你:@soundsdangerious补充道。对所需的行也进行评论。看看这个:@SoundsDangerous嗨,我试过上面的方法。在设置自定义视图后,将setContentView移动到,但它没有移动work@SoundsDangerous找到了答案。这么愚蠢的事。
@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
}