结构
(活动/片段的名称已更改)
根据写在Android Programming: The Big Nerd Ranch Guide中的一些建议,我有以下结构:
MyListFragment extends ListFragment
MyListActivity extends SimpleFragmentActivity
SimpleFragmentActivity extends ActionBarActivity
MyListActivity.java
public class MyListActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
return new MyListFragment();
}
}
SingleFragmentActivity.java
public abstract class SingleFragmentActivity extends ActionBarActivity {
protected abstract Fragment createFragment ();
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
}
}
}
在概述并编写了我的应用程序的基本功能(包括上下文操作栏、导航......)之后,我想确保应用程序尽可能向后兼容,而不需要太多额外的努力。因此,我开始使用 AppCompat (com.android.support:appcompat-v7:20+) ActionBar。
一开始让我感到困惑的是——与我阅读的每个教程或 Android 的开发者指南相反——在 Android Studio 中从未提供过 getSupportActionBar 方法(我使用的是 Android Studio v0.8.2 和 JRE 1.7_60)。每当我键入getActivity().getSup
时,我得到的唯一建议是getSupportFragmentManager
和getSupportLoaderManager
。
作为一种解决方法,我在其中创建一个成员变量ActionBar mActionBar
并MyListFragment
对其进行初始化:
@Override
public void onAttach (Activity activity) {
super.onAttach(activity);
mActionBar = ((ActionBarActivity) activity).getSupportActionBar();
}
这似乎在 Android 4.4.4 (Moto G) 和 2.3.4 (LG E-510) 上都能奏效——这是我唯一可用的设备。
然而,我遇到的问题是,在旋转时,ActionBar 分配丢失,应用程序因 NullPointerException 而崩溃。调试应用程序并在以下行设置断点onCreate
:
mActionBar.setTitle(R.string.title_activity_my_list);
我发现 mActionBar 在旋转后为空。放置所说的线路onCreateView
没有帮助。
我试过的
- 将
mActionBar
in 初始化onAttach
onActivityCreated
onCreate
/onCreateView
就在 .setTitle 被调用之前
我在这里没有想法,感谢您的建议。
EDIT-0 (30.07.)
抛出异常(根据Alex 的建议进行编辑后)
07-30 15:05:25.239 5049-5049/MYAPPNAME E/AndroidRuntime﹕ [Blue Error Handler] Make Debugging Report file for main
java.lang.RuntimeException: Unable to start activity ComponentInfo{MYAPPNAME/MYAPPNAME.MYLISTACTIVITY}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1653)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1669)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2838)
at android.app.ActivityThread.access$1600(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3737)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:894)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at MYAPPNAME.MYLISTFRAGMENT.onCreate(MYLISTFRAGMENT.java:54)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1481)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1896)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:216)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
at MYAPPNAME.SingleFragmentActivity.onCreate(SingleFragmentActivity.java:22)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1617)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1669)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2838)
at android.app.ActivityThread.access$1600(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3737)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:894)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
at dalvik.system.NativeStart.main(Native Method)
MyListFragment
第 54 行(使用 SingleFragmentActivity和ActionBarActivity 作为演员发生异常)
((SingleFragmentActivity) getActivity()).getSupportActionBar().setTitle(R.string
.title_activity_my_list);
SingleFragmentActivity
第 20-22 行:
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);