1

Android 2.3.3

タブレイアウトがあり、タブホストの3つのタブに3つのアクティビティを追加しようとしています。これがコードです。

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="62dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

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

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost>

クラスファイル:::

public class Class_Home_Page extends Activity {

    TabHost tbHost_Home_Page;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home_page);

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

        TabSpec contacts = tbHost_Home_Page.newTabSpec("Contacts");
        contacts.setIndicator("Contacts", getResources().getDrawable(R.drawable.contacts));
        Intent contactsIntent = new Intent(this, Class_Contacts.class);
        contacts.setContent(contactsIntent);

        TabSpec add_contact = tbHost_Home_Page.newTabSpec("Add Contact");
        contacts.setIndicator("Add Contact", getResources().getDrawable(R.drawable.addcontact));
        Intent addContactIntent = new Intent(this, Class_Add_Contact.class);
        contacts.setContent(addContactIntent);

        TabSpec delete_contact = tbHost_Home_Page.newTabSpec("Delete Contact");
        contacts.setIndicator("Delete Contact", getResources().getDrawable(R.drawable.deletecontact));
        Intent deleteContactIntent = new Intent(this, Class_Delete_Contact.class);
        contacts.setContent(deleteContactIntent);

        tbHost_Home_Page.addTab(contacts); // Line with Exception
        tbHost_Home_Page.addTab(add_contact);
        tbHost_Home_Page.addTab(delete_contact);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home_page, menu);
        return true;
    }

}

これは私が得る例外です:::

03-24 21:27:13.030: E/AndroidRuntime(402): FATAL EXCEPTION: main
03-24 21:27:13.030: E/AndroidRuntime(402): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobilevoiceapps.speeddial/com.mobilevoiceapps.speeddial.Class_Home_Page}: java.lang.NullPointerException
03-24 21:27:13.030: E/AndroidRuntime(402):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-24 21:27:13.030: E/AndroidRuntime(402):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-24 21:27:13.030: E/AndroidRuntime(402):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-24 21:27:13.030: E/AndroidRuntime(402):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-24 21:27:13.030: E/AndroidRuntime(402):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 21:27:13.030: E/AndroidRuntime(402):  at android.os.Looper.loop(Looper.java:123)
03-24 21:27:13.030: E/AndroidRuntime(402):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-24 21:27:13.030: E/AndroidRuntime(402):  at java.lang.reflect.Method.invokeNative(Native Method)
03-24 21:27:13.030: E/AndroidRuntime(402):  at java.lang.reflect.Method.invoke(Method.java:507)
03-24 21:27:13.030: E/AndroidRuntime(402):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-24 21:27:13.030: E/AndroidRuntime(402):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-24 21:27:13.030: E/AndroidRuntime(402):  at dalvik.system.NativeStart.main(Native Method)
03-24 21:27:13.030: E/AndroidRuntime(402): Caused by: java.lang.NullPointerException
03-24 21:27:13.030: E/AndroidRuntime(402):  at android.widget.TabHost.addTab(TabHost.java:212)
03-24 21:27:13.030: E/AndroidRuntime(402):  at com.mobilevoiceapps.speeddial.Class_Home_Page.onCreate(Class_Home_Page.java:37)
03-24 21:27:13.030: E/AndroidRuntime(402):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-24 21:27:13.030: E/AndroidRuntime(402):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-24 21:27:13.030: E/AndroidRuntime(402):  ... 11 more

何が問題なのですか?

提案されたようにコードを変更しました。それでもエラーが発生します...

public class Class_Home_Page extends Activity {

    TabHost tbHost_Home_Page;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home_page);

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

        TabSpec contacts = tbHost_Home_Page.newTabSpec("Contacts");
        contacts.setIndicator("Contacts", getResources().getDrawable(R.drawable.contacts));
        Intent contactsIntent = new Intent(this, Class_Contacts.class);
        contacts.setContent(contactsIntent);

        TabSpec add_contact = tbHost_Home_Page.newTabSpec("Add Contact");
        add_contact.setIndicator("Add Contact", getResources().getDrawable(R.drawable.addcontact));
        Intent addContactIntent = new Intent(this, Class_Add_Contact.class);
        add_contact.setContent(addContactIntent);

        TabSpec delete_contact = tbHost_Home_Page.newTabSpec("Delete Contact");
        delete_contact.setIndicator("Delete Contact", getResources().getDrawable(R.drawable.deletecontact));
        Intent deleteContactIntent = new Intent(this, Class_Delete_Contact.class);
        delete_contact.setContent(deleteContactIntent);

        tbHost_Home_Page.addTab(contacts);
        tbHost_Home_Page.addTab(add_contact);
        tbHost_Home_Page.addTab(delete_contact);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home_page, menu);
        return true;
    }

}

これがエラーです..同じ行に

03-24 21:49:01.330: E/AndroidRuntime(499): FATAL EXCEPTION: main
03-24 21:49:01.330: E/AndroidRuntime(499): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobilevoiceapps.speeddial/com.mobilevoiceapps.speeddial.Class_Home_Page}: java.lang.NullPointerException
03-24 21:49:01.330: E/AndroidRuntime(499):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-24 21:49:01.330: E/AndroidRuntime(499):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-24 21:49:01.330: E/AndroidRuntime(499):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-24 21:49:01.330: E/AndroidRuntime(499):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-24 21:49:01.330: E/AndroidRuntime(499):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 21:49:01.330: E/AndroidRuntime(499):  at android.os.Looper.loop(Looper.java:123)
03-24 21:49:01.330: E/AndroidRuntime(499):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-24 21:49:01.330: E/AndroidRuntime(499):  at java.lang.reflect.Method.invokeNative(Native Method)
03-24 21:49:01.330: E/AndroidRuntime(499):  at java.lang.reflect.Method.invoke(Method.java:507)
03-24 21:49:01.330: E/AndroidRuntime(499):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-24 21:49:01.330: E/AndroidRuntime(499):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-24 21:49:01.330: E/AndroidRuntime(499):  at dalvik.system.NativeStart.main(Native Method)
03-24 21:49:01.330: E/AndroidRuntime(499): Caused by: java.lang.NullPointerException
03-24 21:49:01.330: E/AndroidRuntime(499):  at android.widget.TabHost.addTab(TabHost.java:212)
03-24 21:49:01.330: E/AndroidRuntime(499):  at com.mobilevoiceapps.speeddial.Class_Home_Page.onCreate(Class_Home_Page.java:37)
03-24 21:49:01.330: E/AndroidRuntime(499):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-24 21:49:01.330: E/AndroidRuntime(499):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-24 21:49:01.330: E/AndroidRuntime(499):  ... 11 more
4

1 に答える 1

2

setContentsetIndicatorを設定するのadd_contactを忘れますdelete_contact。コードを次のように変更します:

 TabSpec add_contact = tbHost_Home_Page.newTabSpec("Add Contact");
 // use add_contact instead of contacts
 add_contact.setIndicator("Add Contact", 
                      getResources().getDrawable(R.drawable.addcontact));
 Intent addContactIntent = new Intent(this, Class_Contacts.class);
 // use add_contact instead of contacts
 add_contact.setContent(addContactIntent);


 TabSpec delete_contact = tbHost_Home_Page.newTabSpec("Delete Contact");
  // use delete_contact instead of contacts
 delete_contact.setIndicator("Delete Contact", 
                    getResources().getDrawable(R.drawable.deletecontact));
 Intent deleteContactIntent = new Intent(this, Class_Contacts.class);
 // use delete_contact instead of contacts
 delete_contact.setContent(deleteContactIntent);
于 2013-03-24T16:11:56.700 に答える