应用程序在android中单击选项卡时崩溃

时间:2016-06-22 14:41:05

标签: android tabwidget

我正在尝试创建一个涉及标签的应用,第一个标签工作正常,但点击第二个标签应用程序崩溃。我在SO上查了很多,并尝试了建议,没有人帮忙。请查看代码并让我知道错误。提前致谢

以下是主要活动代码:

public class MainActivity extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         TabHost tabHost = getTabHost();

            // Tab for data entry
            TabSpec data_entryspec = tabHost.newTabSpec("Enter Data");
            // setting Title and Icon for the Tab
            data_entryspec.setIndicator("Data Entry");
            Intent dataIntent = new Intent(this, DataEntry.class);
            data_entryspec.setContent(dataIntent);

            // Tab for statistics
            TabSpec check_resultspec = tabHost.newTabSpec("See Statistics");        
            check_resultspec.setIndicator("See Statistics");
            Intent statisticsIntent = new Intent(this, ShowResult.class);
            check_resultspec.setContent(statisticsIntent);


            // Adding all TabSpec to TabHost
            tabHost.addTab(data_entryspec); // Adding data tab
            tabHost.addTab(check_resultspec); // Adding statistics tab
    }

    }

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>
</TabHost>

logcat的:

06-22 20:05:12.217: E/AndroidRuntime(2461): FATAL EXCEPTION: main
06-22 20:05:12.217: E/AndroidRuntime(2461): java.lang.RuntimeException: Unable to start activity ComponentInfo{colg.project.healthapp/colg.project.healthapp.ShowResult}: java.lang.NullPointerException
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1797)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.widget.TabHost.setCurrentTab(TabHost.java:346)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:540)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.view.View.performClick(View.java:3511)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.view.View$PerformClick.run(View.java:14105)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.os.Handler.handleCallback(Handler.java:605)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.os.Looper.loop(Looper.java:137)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at java.lang.reflect.Method.invokeNative(Native Method)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at java.lang.reflect.Method.invoke(Method.java:511)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at dalvik.system.NativeStart.main(Native Method)
06-22 20:05:12.217: E/AndroidRuntime(2461): Caused by: java.lang.NullPointerException
06-22 20:05:12.217: E/AndroidRuntime(2461):     at colg.project.healthapp.ShowResult.onCreate(ShowResult.java:35)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.app.Activity.performCreate(Activity.java:4466)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-22 20:05:12.217: E/AndroidRuntime(2461):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-22 20:05:12.217: E/AndroidRuntime(2461):     ... 18 more

0 个答案:

没有答案