0

我正在处理选项卡,每个选项卡都显示一个列表视图

我的代码如下:(我只显示第一个选项卡的代码)任何人都可以帮助我哪里出错了吗?

我的XML文件如下

<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
        android:id="@+id/tab1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="60dp" >

        <ListView
            android:id="@+id/pcards_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </ListView>
    </LinearLayout>
</FrameLayout>

我的 MAIN.XML 如下

IT 没有显示任何错误,但是当我在模拟器上运行它时程序仍然崩溃

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        TabHost mtabhost =(TabHost)findViewById(R.id.tabHost);
        mtabhost.setup();

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

        ListView  pcards_list= (ListView)findViewById(R.id.pcards_list);
        ArrayList<String> pcards_arraylist = new ArrayList<String>();



        ArrayAdapter<String> adapter1= new  ArrayAdapter<String(this,android.R.layout.simple_list_item_2,pcards_arraylist);
        pcards_list.setAdapter(adapter1);

        mtabhost.addTab(spec1);

    }
}

请帮帮我!

任何人都可以为我提供一个很好的关于标签的教程以及使用具有列表视图的片段的标签吗?先感谢您

4

4 回答 4

1

这是一个很好的教程:http ://android.codeandmagic.org/2011/07/android-tabs-with-fragments/ 另请查看:http ://thepseudocoder.wordpress.com/2011/10/04/android-tabs -片段方式/

于 2013-02-27T10:19:44.297 回答
1

我从 Travis (84 - 86) 学习了教程中的选项卡。不确定它是否真的有帮助,而你之前没有做过其余的事情。

http://thenewboston.org/watch.php?cat=6&number=84

于 2013-02-27T10:20:46.623 回答
1

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

        <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
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <ListView
                    android:id="@+id/myList"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >
                </ListView>
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost>

AndroidTabActivity.java

public class AndroidTabActivity extends TabActivity {

    ListView mList;

    String[] myContacts = { "Anirudh", "Sonia", "Raj", "Sachin" };

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

        mList = (ListView) findViewById(R.id.myList);

        ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, myContacts);

        mList.setAdapter(myAdapter);


        TabHost tabHost = getTabHost();

        // Tab for Contact
        TabSpec contactspec = tabHost.newTabSpec("Contact");
        // setting Title for the Tab
        contactspec.setIndicator("Contact");
        contactspec.setContent(R.id.tab1);

        // Tab for Song
        TabSpec songspec = tabHost.newTabSpec("Song");
        songspec.setIndicator("Songs");
        Intent songIntent = new Intent(this, SongActivity.class);
        songspec.setContent(songIntent);


        // Adding all TabSpec to TabHost
        tabHost.addTab(contactspec); // Adding contact tab
        tabHost.addTab(songspec); // Adding song tab

    }
}

输出

通过这种方式,您可以创建选项卡并设置适当Activity的内容。

希望这可以帮助。

于 2013-02-27T10:24:04.943 回答
0

ActivityAndroidManifest.xml???application如果没有,请在标签下添加权限,如下所示:

 <activity android:name="MainActivity"/> 
于 2013-02-27T10:18:33.020 回答