在调用addTab(TabHost.TabSpec规格)设立tabhost空指针异常的方法 [英] nullpointer exception in setting up a tabhost at calling addTab( TabHost.TabSpec spec) method
问题描述
这个问题实在是怪异和驾驶我疯了。
我无法摆脱的NullPointerException异常的。
我的任务很简单,只要运行在tabhost测试。但现在我不能,甚至创建一个tabhost!
为了ceate一个tabhost,有我需要小心一些要点。
首先都必须有一个ID的tabwidget@android:ID /制表符等的FrameLayout其中ID为@android:ID / tabconent。我发誓我千万要注意,你可以看到,在我的$ C $以下CS。
和我读我的文章使用TabActivity recommanding。但不幸的是这种tabhost将是活动的一个组成部分。所以我有一个正常的活动创建它。
当code变为addSpec(),它抛出一个NullPointerException异常。我考察无论是在logcat中的tabHost和Host.Spec。他们既不是空!我将我印出来的结果添加到这个问题的结束。
这是我的java code。
公共类曹延伸活动{
滚动型menu_scroll;
TabHost menu_host;
TabWidget menu_tags;
INT []的内容;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.testtab);
menu_host =(TabHost)findViewById(R.id.tabhost);
Log.e(tabhost(menu_host == NULL)+);
menu_tags =(TabWidget)findViewById(android.R.id.tabs);
内容= INT新[] {R.id.b1,R.id.b2};
诠释计数= 6;
的for(int i = 0; I<计数;我++){
则tabspec规格= menu_host.newTabSpec(1 +);
spec.setIndicator(否+ I);
Log.e(规范(规范== NULL)+);
spec.setContent(R.id.b1); ** //发生在这条线的问题**
menu_host.addTab(规范);
}
}
}
和我的XML code是如下:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>< TabHost
机器人:ID =@ + ID / tabhost
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT> <的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =50dp
机器人:方向=垂直> < HorizontalScrollView
机器人:ID =@ + ID / menu_scroll
机器人:layout_width =FILL_PARENT
机器人:layout_height =50dp
机器人:滚动条=无> < TabWidget
机器人:ID =@机器人:ID /标签
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT>
< / TabWidget>
< / HorizontalScrollView>
< / LinearLayout中> <的FrameLayout
机器人:ID =@机器人:ID / tabcontent
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT> <按钮
机器人:ID =@ + ID / B1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=1/> <按钮
机器人:ID =@ + ID / B2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=2/> < /&的FrameLayout GT;
< / TabHost>< / LinearLayout中>
此外,logcat中打印出
tabhost(490):假
规格(490):假
E / AndroidRuntime(490):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
E / AndroidRuntime(490):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.tim.wirelessorder / com.tim.wirelessorder.ui.Cao}:显示java.lang.NullPointerException
E / AndroidRuntime(490):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E / AndroidRuntime(490):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E / AndroidRuntime(490):在android.app.ActivityThread.access $ 2200(ActivityThread.java:119)
E / AndroidRuntime(490):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1863)
E / AndroidRuntime(490):在android.os.Handler.dispatchMessage(Handler.java:99)
E / AndroidRuntime(490):在android.os.Looper.loop(Looper.java:123)
E / AndroidRuntime(490):在android.app.ActivityThread.main(ActivityThread.java:4363)
E / AndroidRuntime(490):在java.lang.reflect.Method.invokeNative(本机方法)
E / AndroidRuntime(490):在java.lang.reflect.Method.invoke(Method.java:521)
E / AndroidRuntime(490):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)
E / AndroidRuntime(490):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E / AndroidRuntime(490):在dalvik.system.NativeStart.main(本机方法)
E / AndroidRuntime(490):显示java.lang.NullPointerException:产生的原因
E / AndroidRuntime(490):在android.widget.TabHost $ ViewIdContentStrategy<&初始化GT;(TabHost.java:583)。
E / AndroidRuntime(490):在android.widget.TabHost $ ViewIdContentStrategy<&初始化GT;(TabHost.java:578)。
E / AndroidRuntime(490):在android.widget.TabHost $ TabSpec.setContent(TabHost.java:435)
E / AndroidRuntime(490):在com.tim.wirelessorder.ui.Cao.onCreate(Cao.java:38)
E / AndroidRuntime(490):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E / AndroidRuntime(490):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
您需要延长TabActivity不是简单的活动
尝试
公共类曹扩展TabActivity
的这一翻译
公共类曹扩展活动
和改变这种
menu_host =(TabHost)findViewById(R.id.tabhost);
要
menu_host = getTabHost();
和
机器人:ID =@ + ID / tabhost
到
机器人:ID =@机器人:ID / tabhost
this question is really wierd and driving me crazy. i can't get rid of the nullPointerException. my task is quite simple, just run a test on the tabhost. but now i'm not able to even create one tabhost! in order to ceate a tabhost, there some points i need to be careful with. first there must be a tabwidget with an id of "@android:id/tabs" and a framelayout of which the id is "@android:id/tabconent". i swear i do pay attention and you can see that in my codes below. and i read my articles recommanding using TabActivity. but unfortunately this tabhost will be a component of an activity. so i have to create it in a normal activity.
when the code goes to "addSpec()", it throws a nullPointerException. i examine both the tabHost and Host.Spec in the Logcat. neither of them is null! i will add my print-out result to the end of this question.
here is my java code.
public class Cao extends Activity {
ScrollView menu_scroll;
TabHost menu_host;
TabWidget menu_tags;
int[] contents;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testtab);
menu_host = (TabHost) findViewById(R.id.tabhost);
Log.e("tabhost", (menu_host == null) + "");
menu_tags = (TabWidget) findViewById(android.R.id.tabs);
contents = new int[] { R.id.b1, R.id.b2 };
int count = 6;
for (int i = 0; i < count; i++) {
TabSpec spec = menu_host.newTabSpec(i + "");
spec.setIndicator("no " + i);
Log.e("spec", (spec == null) + "");
spec.setContent(R.id.b1); **// the problem occurs at this line**
menu_host.addTab(spec);
}
}
}
and my xml code is as follows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="vertical" >
<HorizontalScrollView
android:id="@+id/menu_scroll"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:scrollbars="none" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget>
</HorizontalScrollView>
</LinearLayout>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
</FrameLayout>
</TabHost>
</LinearLayout>
in addition the logcat print-out
tabhost(490): false
spec(490): false
E/AndroidRuntime(490): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tim.wirelessorder/com.tim.wirelessorder.ui.Cao}: java.lang.NullPointerException
E/AndroidRuntime(490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E/AndroidRuntime(490): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime(490): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime(490): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime(490): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(490): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(490): at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime(490): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(490): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(490): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime(490): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime(490): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(490): Caused by: java.lang.NullPointerException
E/AndroidRuntime(490): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:583)
E/AndroidRuntime(490): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:578)
E/AndroidRuntime(490): at android.widget.TabHost$TabSpec.setContent(TabHost.java:435)
E/AndroidRuntime(490): at com.tim.wirelessorder.ui.Cao.onCreate(Cao.java:38)
E/AndroidRuntime(490): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
you need to extend TabActivity not simple activity try
public class Cao extends TabActivity
intead of
public class Cao extends Activity
and change this
menu_host = (TabHost) findViewById(R.id.tabhost);
to menu_host =getTabHost(); and
android:id="@+id/tabhost"
to
android:id="@android:id/tabhost"
这篇关于在调用addTab(TabHost.TabSpec规格)设立tabhost空指针异常的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!