0

3 つのタブ (Tab1、タブ 2、タブ 3) があります。すべてのタブで他のアクティビティを開始したい。タブ 2 で新しいインテントを試します。

私のコードこれ

  public class MainActivity extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
            tabHost.setup();




                    TabSpec spec1=tabHost.newTabSpec("Tab 1");
            spec1.setContent(R.id.tab1);
            spec1.setIndicator("Tab 1");



            TabSpec spec2=tabHost.newTabSpec("Tab 2");
            spec2.setIndicator("Tab 2", getResources().getDrawable(R.drawable.ic_launcher));        
            spec2.setContent(new Intent(this,teszt.class));


            TabSpec spec3=tabHost.newTabSpec("Tab 3");
            spec3.setIndicator("Tab 3");
            spec3.setContent(R.id.tab3);

            tabHost.addTab(spec1);
            tabHost.addTab(spec2);
            tabHost.addTab(spec3);
            }

    }

マニフェスト.xml

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="hu.anzy.fulek.example.fulek.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".teszt"
            ></activity>
    </application>

Test.java

public class testzt extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.teszt_layout);

}

public void onClick(View v){
    if (v.getId()== R.id.btnBackTo1){
        Toast.makeText(this, "Click BackTo1", Toast.LENGTH_LONG).show();
    } else
    {
        if (v.getId()== R.id.btnStart3) {
            Toast.makeText(this, "Click Start3", Toast.LENGTH_LONG).show();

        }
    }


}

}

Main.xml

<?xml version="1.0" encoding="utf-8"?>

    <TabHost android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabHost"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <TabWidget
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/tabs"
    />
     <FrameLayout
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabcontent"
     >
     <LinearLayout
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tab1"
    android:orientation="vertical"
    android:paddingTop="60px"
     >
     <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="100px" 
    android:text="This is tab1"
    android:id="@+id/txt1"
    />    

     </LinearLayout>

     <LinearLayout
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tab2"
    android:orientation="vertical"
    android:paddingTop="60px"
     >


     </LinearLayout>

      <LinearLayout
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tab3"
    android:orientation="vertical"
    android:paddingTop="60px"
     >
     <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="100px" 
    android:text="This is tab 3"
    android:id="@+id/txt3"
    />

     </LinearLayout>
     </FrameLayout>

    </TabHost>

Test_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

       <Button
        android:id="@+id/btnBackTo1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Back to 1st Activity"
        android:onClick="onClick"
     />
     <Button
          android:id="@+id/btnStart3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Start 3rd Activity"
        android:onClick="onClick"
     />

</LinearLayout>

アプリケーションは実行中ですが、Tab 2 を選択するとアプリケーションが停止します。

何が問題ですか?

4

1 に答える 1