当我按照本教程创建单独的项目以在 Android 中创建带有操作栏的选项卡布局时,它工作正常。
但是,当我尝试通过单击按钮从主要活动加载选项卡式活动时,应用程序会崩溃。我在 Main 活动的 Tabbed 活动中调用 onCreate,如下所示:
Button ListBtn = (Button) findViewById(R.id.main_btn);
ListBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Tabbed.class));
}
});
日志猫
05-02 16:43:39.879: D/gralloc_goldfish(4337): Emulator without GPU emulation detected.
05-02 16:43:39.909: I/dalvikvm(4337): threadid=3: reacting to signal 3
05-02 16:43:39.929: I/dalvikvm(4337): Wrote stack traces to '/data/anr/traces.txt'
05-02 16:43:42.309: D/AndroidRuntime(4337): Shutting down VM
05-02 16:43:42.309: W/dalvikvm(4337): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-02 16:43:42.339: E/AndroidRuntime(4337): FATAL EXCEPTION: main
05-02 16:43:42.339: E/AndroidRuntime(4337): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.T/com.
AppName.Tabbed}:
java.lang.NullPointerException
05-02 16:43:42.339: E/AndroidRuntime(4337): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-02 16:43:42.339: E/AndroidRuntime(4337): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-02 16:43:42.339: E/AndroidRuntime(4337): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-02 16:43:42.339: E/AndroidRuntime(4337): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-02 16:43:42.339: E/AndroidRuntime(4337): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 16:43:42.339: E/AndroidRuntime(4337): at android.os.Looper.loop(Looper.java:137)
05-02 16:43:42.339: E/AndroidRuntime(4337): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-02 16:43:42.339: E/AndroidRuntime(4337): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 16:43:42.339: E/AndroidRuntime(4337): at java.lang.reflect.Method.invoke(Method.java:511)
05-02 16:43:42.339: E/AndroidRuntime(4337): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-02 16:43:42.339: E/AndroidRuntime(4337): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-02 16:43:42.339: E/AndroidRuntime(4337): at dalvik.system.NativeStart.main(Native Method)
05-02 16:43:42.339: E/AndroidRuntime(4337): Caused by: java.lang.NullPointerException
05-02 16:43:42.339: E/AndroidRuntime(4337): at packageName.Tabbed.onCreate(Tabbed.java:17)
05-02 16:43:42.339: E/AndroidRuntime(4337): at android.app.Activity.performCreate(Activity.java:4465)
05-02 16:43:42.339: E/AndroidRuntime(4337): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-02 16:43:42.339: E/AndroidRuntime(4337): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-02 16:43:42.339: E/AndroidRuntime(4337): ... 11 more
05-02 16:43:42.979: I/dalvikvm(4337): threadid=3: reacting to signal 3
05-02 16:43:43.009: I/dalvikvm(4337): Wrote stack traces to '/data/anr/traces.txt'
05-02 16:43:43.319: I/dalvikvm(4337): threadid=3: reacting to signal 3
05-02 16:43:43.349: I/dalvikvm(4337): Wrote stack traces to '/data/anr/traces.txt'
Oncreate 方法
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
String label1 = getResources().getString(R.string.label1);
Tab tab = actionBar.newTab();
tab.setText(label1);
TabListener<Tab1Fragment> tl = new TabListener<Tab1Fragment>(this,
label1, Tab1Fragment.class);
tab.setTabListener(tl);
actionBar.addTab(tab);
String label2 = getResources().getString(R.string.label2);
tab = actionBar.newTab();
tab.setText(label2);
TabListener<Tab2Fragment> tl2 = new TabListener<Tab2Fragment>(this,
label2, Tab2Fragment.class);
tab.setTabListener(tl2);
actionBar.addTab(tab);
}