如何实现具有制表符活动的按钮

All*_*der 1 android android-intent

我有一个标签活动,然后在其中一个活动中,我想放置一个按钮来进行不同的活动.但是,当我编码按钮(没有错误)但启动应用程序时,它崩溃与错误代码:

11-14 12:50:43.783: E/AndroidRuntime(10933): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dist.distguide/com.dist.distguide.MainActivity}: java.lang.NullPointerException

但是当我删除按钮编码时,应用程序启动正常.

这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/logo1">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp" >

        <Button
            android:id="@+id/Distancecalc"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Distance Calculator" />

        <Button
            android:id="@+id/Distance"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Distance Calculator" />

    </FrameLayout>

  </LinearLayout>
 </TabHost>
Run Code Online (Sandbox Code Playgroud)

然后是我的Java:

Intent intentHome = new Intent().setClass(this, HomeActivity.class);
    TabSpec tabSpecHome = tabHost
        .newTabSpec("Home")
        .setIndicator("", ressources.getDrawable(R.drawable.icon_home_config))
        .setContent(intentHome);
    Button add = (Button)findViewById(R.id.Distance1);
    add.bringToFront(); 
    add.setOnClickListener(new View.OnClickListener()
    {
    public void onClick(View v)
    {
         Intent myIntent = new Intent(MainActivity.this,DistanceCalc.class);
            startActivity(myIntent);  
         }
    });
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?请帮忙!谢谢

M D*_*M D 5

更改

Button add = (Button)findViewById(R.id.Distance1);
Run Code Online (Sandbox Code Playgroud)

Button add = (Button)findViewById(R.id.Distancecalc);
Run Code Online (Sandbox Code Playgroud)

你的按钮ID是Distancecalc.而你正试图用它来初始化它Distance1.