0

我有三个选项卡(选项卡 1、选项卡 2、选项卡 3)。我想在所有选项卡中启动其他活动。我在 Tab 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>

测试.java

公共类 testzt 扩展 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();

        }
    }


}

}

主.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>

测试布局.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

0

好的,我找到了解决方案。

   public class MainActivity extends TabActivity{

      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            TabHost tabHost = getTabHost();

            // Tab1
            TabSpec spec1 = tabHost.newTabSpec("Tab1");

            photospec.setIndicator("Tab1");
            Intent aIntent = new Intent(this, aActivity.class);
            photospec.setContent(aIntent);

            // Tab2
            TabSpec spec2 = tabHost.newTabSpec("Tab2");
            songspec.setIndicator("Tab2");
            Intent bIntent = new Intent(this, bActivity.class);
            songspec.setContent(bIntent);

            // Tab3
            TabSpec spec3 = tabHost.newTabSpec("Tab3");
            spec3.setIndicator("Tab3");
            Intent cIntent = new Intent(this, cActivity.class);
            spec3.setContent(cIntent);

            // Adding all TabSpec to TabHost
            tabHost.addTab(spec1); // Adding tab
            tabHost.addTab(spec2); // Adding tab
            tabHost.addTab(spec3); // Adding tab
        }
    }

这个aActivity类似于bActivity和cActivity

import android.app.Activity;
import android.os.Bundle;

public class aActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_layout);
    }
}

A_layout.xml 类似 B_layout.xml 和 C_layout.xml

   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">


      <TextView android:text=" Text"
                android:padding="15dip"
                android:textSize="18dip"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>
    </LinearLayout>

主要的.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">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <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"/>
        </LinearLayout>
    </TabHost>

显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hu.anzy.example.androidtablayoutactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="hu.anzy.example.MaintActivity"
            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=".aActivity" />


        <activity android:name=".bActivity" />


        <activity android:name=".cActivity" />
    </application>

</manifest>
于 2013-04-20T19:29:02.907 回答