NullPointerException wit Simple TabActivity

时间:2011-06-15 14:55:11

标签: java android nullpointerexception android-sdk-2.3

我刚刚开始为Android开发并且在我制作第一个应用程序时非常兴奋(完全理解它或者我认为),但我无法让它运行。它看起来很干净,我不知道根据调试信息要查找什么。至少要解释一下我应该寻找什么以及为什么会非常感激。我可以提供您认为有用的任何其他代码。

谢谢。

这是我的主要课程:

package com.couchcode.realtime;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

public class Realtime extends TabActivity {
    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

        makeTab("test");
    }
    private void makeTab(String tabTag) {
        intent = new Intent().setClass(this, SearchTab.class);
        View tabview = createTabView(tabHost.getContext(), tabTag);
        spec = tabHost.newTabSpec(tabTag).setIndicator(tabview).setContent(intent);
        tabHost.addTab(spec);
        tabHost.setCurrentTabByTag(tabTag);
    }       
    private static View createTabView(final Context context, final String text) {
            View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
            TextView tv = (TextView) view.findViewById(R.id.tabsText);
            tv.setText(text);
            return view;
        }
} 

这是运行调试器时logcat的输出:

06-15 07:35:33.447: WARN/dalvikvm(25209): threadid=1: thread exiting with uncaught exception (group=0x401bd560)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209): FATAL EXCEPTION: main
06-15 07:35:33.477: ERROR/AndroidRuntime(25209): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.couchcode.realtime/com.couchcode.realtime.Realtime}: java.lang.NullPointerException
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1680)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at android.os.Looper.loop(Looper.java:130)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at android.app.ActivityThread.main(ActivityThread.java:3835)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at java.lang.reflect.Method.invokeNative(Native Method)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at java.lang.reflect.Method.invoke(Method.java:507)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at dalvik.system.NativeStart.main(Native Method)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209): Caused by: java.lang.NullPointerException
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at android.app.Activity.setContentView(Activity.java:1657)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at android.app.TabActivity.ensureTabHost(TabActivity.java:114)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at android.app.TabActivity.getTabHost(TabActivity.java:136)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at com.couchcode.realtime.Realtime.(Realtime.java:14)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at java.lang.Class.newInstanceImpl(Native Method)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at java.lang.Class.newInstance(Class.java:1409)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1672)
06-15 07:35:33.477: ERROR/AndroidRuntime(25209):     ... 11 more

1 个答案:

答案 0 :(得分:5)

你不能在任何地方调用getTabHost,这在你的构造函数中是有效的。您必须在onCreate()方法中执行此操作:

public class Realtime extends TabActivity {
    TabHost tabHost;
    TabHost.TabSpec spec;
    Intent intent;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tabHost = getTabHost();
        tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

        makeTab("test");
    }