English 中文(简体)
调用 addTab( TabHost. TabSpec spec) 方法时设置制表主机时的无效指定器例外
原标题:nullpointer exception in setting up a tabhost at calling addTab( TabHost.TabSpec spec) method

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.

当代码进入“ addspec () ” 时, 它会抛出一个无效的点数例外 。 我检查标签 Host 和 Host 。 请在 Logcat 中检查 。 两者都不是无效的! 我将在问题结尾处添加我的打印输出结果 。

这是我的java代码。

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);
    }
}
}

我的xml 代码如下

<?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>

此外,对数打印出

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)
问题回答

将您的标签主机id改成

<TabHost 
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

/你的活动 延伸 缩放 像这样的活动 低于1

public class HelloTabWidget extends TabActivity {

在你的Create上做这个

 TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

< a href="""http:// developer.android.com/resources/tutors/tutorics/views/ hello-tabwidget.html" rel="nofollow" 请选中:

you need 至extend TabActivity not simple activity try

public class Cao extends TabActivity 

整数

public class Cao extends Activity 

更改此变化

menu_host = (TabHost) findViewById(R.id.tabhost);

至 menu_host =getTabHost(); and

 android:id="@+id/tabhost"

android:id="@android:id/tabhost"

我所需要的只是改变我在.xml中宣布 TabHost 的代号的方式。 我使用的不是 "id/tabhost" ,而是 @android:id/tabhost> 。然后,在指定代码中的 TabHost 时,我称之为 findVibById( and mid.R.id.id.tabhost) 。这无论出于何种原因,都解决了我的问题。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签