1

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

谢谢你。

这是我的主要课程:

包 com.couchcode.realtime;

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

公共类实时扩展 TabActivity {
    TabHost tabHost = getTabHost();
    TabHost.TabSpec 规范;
    意图意图;

    /** 在第一次创建活动时调用。*/
    @覆盖
    公共无效 onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        设置内容视图(R.layout.main);

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

        makeTab("测试");
    }
    私人无效makeTab(字符串tabTag){
        intent = new Intent().setClass(this, SearchTab.class);
        查看 tabview = createTabView(tabHost.getContext(), tabTag);
        规格 = tabHost.newTabSpec(tabTag).setIndicator(tabview).setContent(intent);
        tabHost.addTab(spec);
        tabHost.setCurrentTabByTag(tabTag);
    }       
    private static View createTabView(final Context context, final String text) {
            查看视图 = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
            TextView tv = (TextView) view.findViewById(R.id.tabsText);
            tv.setText(文本);
            返回视图;
        }
}

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

06-15 07:35:33.447: WARN/dalvikvm(25209): threadid=1: 线程以未捕获的异常退出(组=0x401bd560)
06-15 07:35:33.477:错误/AndroidRuntime(25209):致命异常:主要
06-15 07:35:33.477: 错误/AndroidRuntime(25209): java.lang.RuntimeException: 无法实例化活动 ComponentInfo{com.couchcode.realtime/com.couchcode.realtime.Realtime}: java.lang.NullPointerException
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1680)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 android.app.ActivityThread.access$1500(ActivityThread.java:123)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 android.os.Handler.dispatchMessage(Handler.java:99)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 android.os.Looper.loop(Looper.java:130)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 android.app.ActivityThread.main(ActivityThread.java:3835)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 java.lang.reflect.Method.invokeNative(Native Method)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 java.lang.reflect.Method.invoke(Method.java:507)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-15 07:35:33.477:错误/AndroidRuntime(25209):在 dalvik.system.NativeStart.main(本机方法)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 由: java.lang.NullPointerException 引起
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 android.app.Activity.setContentView(Activity.java:1657)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 android.app.TabActivity.ensureTabHost(TabActivity.java:114)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 android.app.TabActivity.getTabHost(TabActivity.java:136)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 com.couchcode.realtime.Realtime.(Realtime.java:14)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 java.lang.Class.newInstanceImpl(Native Method)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 java.lang.Class.newInstance(Class.java:1409)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1672)
06-15 07:35:33.477: 错误/AndroidRuntime(25209): ... 11 更多
4

1 回答 1

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");
    }
于 2011-06-15T15:00:55.453 回答