gpt4 book ai didi

java - NullPointerException 机智简单 TabActivity

转载 作者:行者123 更新时间:2023-11-29 08:13:33 24 4
gpt4 key购买 nike

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

谢谢。

这是我的主要类(class):

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: main06-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.NullPointerException06-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.NullPointerException06-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

最佳答案

您不能在您所在的位置调用 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");
}

关于java - NullPointerException 机智简单 TabActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6359741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com