【问题标题】:Tab (activity) of tabhost shows nothingtabhost 的选项卡(活动)什么也不显示
【发布时间】:2013-11-24 18:33:02
【问题描述】:

该应用有 5 个选项卡,第一个选项卡的布局中只有一个 TextView,其他选项卡的布局与 ListView 相同,但它在选项卡中没有显示任何内容,只有白色背景。

这里是一些日志和代码:

控制台:

ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=tbf.tb.simulador1o1/.MainActivity }
ActivityManager: Warning: Activity not started, its current task has been brought to the front

LogCat:

11-24 18:13:11.211: D/dalvikvm(1790): GC_FOR_ALLOC freed 392K, 10% free 9501K/10476K, paused 97ms, total 121ms
11-24 18:13:11.341: D/dalvikvm(1790): GC_FOR_ALLOC freed 6863K, 68% free 3848K/11688K, paused 41ms, total 42ms
11-24 18:13:11.341: I/dalvikvm-heap(1790): Grow heap (frag case) to 4.559MB for 696976-byte allocation
11-24 18:13:11.471: D/dalvikvm(1790): GC_FOR_ALLOC freed <1K, 62% free 4528K/11688K, paused 119ms, total 119ms
11-24 18:13:11.551: D/dalvikvm(1790): GC_CONCURRENT freed 0K, 62% free 4528K/11688K, paused 16ms+17ms, total 87ms
11-24 18:13:11.672: D/dalvikvm(1790): GC_FOR_ALLOC freed <1K, 62% free 4528K/11688K, paused 46ms, total 46ms
11-24 18:13:11.672: I/dalvikvm-heap(1790): Grow heap (frag case) to 5.739MB for 1238176-byte allocation
11-24 18:13:11.751: D/dalvikvm(1790): GC_CONCURRENT freed 0K, 51% free 5737K/11688K, paused 10ms+16ms, total 81ms
11-24 18:13:11.921: D/dalvikvm(1790): GC_FOR_ALLOC freed 1032K, 52% free 5683K/11688K, paused 39ms, total 41ms
11-24 18:13:12.111: D/dalvikvm(1790): GC_FOR_ALLOC freed 352K, 47% free 6309K/11688K, paused 38ms, total 39ms
11-24 18:13:12.231: D/dalvikvm(1790): GC_FOR_ALLOC freed 352K, 41% free 6934K/11688K, paused 38ms, total 39ms
11-24 18:13:12.482: D/dalvikvm(1790): GC_FOR_ALLOC freed 352K, 33% free 7913K/11688K, paused 41ms, total 42ms
11-24 18:13:36.001: I/Choreographer(1790): Skipped 57 frames!  The application may be doing too much work on its main thread.

MainActivity 布局:

<?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="match_parent"
    android:layout_height="match_parent">

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

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

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>
</TabHost>

MainActivity 类:

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

        // TabHost tabHost = getTabHost();
        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

        // Setting tabs
        TabSpec tab1Spec = tabHost.newTabSpec("shopping");
        tab1Spec.setIndicator("", getResources().getDrawable(R.drawable.shopping_selector));
        tab1Spec.setContent(new Intent(this, Tab_1.class));

        TabSpec tab2Spec = tabHost.newTabSpec("combo");
        tab2Spec.setIndicator("", getResources().getDrawable(R.drawable.combo_selector));
        tab2Spec.setContent(new Intent(this, Tab_2.class));

        TabSpec tab3Spec = tabHost.newTabSpec("sandwich");
        tab3Spec.setIndicator("", getResources().getDrawable(R.drawable.sandwich_selector));
        tab3Spec.setContent(new Intent(this, Tab_3.class));

        TabSpec tab4Spec = tabHost.newTabSpec("dog_chip_natural");
        tab4Spec.setIndicator("", getResources().getDrawable(R.drawable.dogchipnatural_selector));
        tab4Spec.setContent(new Intent(this, Tab_4.class));

        TabSpec tab5Spec = tabHost.newTabSpec("drink_acai_icecream");
        tab5Spec.setIndicator("", getResources().getDrawable(R.drawable.drinkacaiicecream_selector));
        tab5Spec.setContent(new Intent(this, Tab_5.class));

        // Adding all TabSpec to TabHost
        tabHost.addTab(tab1Spec);
        tabHost.addTab(tab2Spec);
        tabHost.addTab(tab3Spec);
        tabHost.addTab(tab4Spec);
        tabHost.addTab(tab5Spec);
    }
}

Tab_2 布局:

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

    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="0" >
    </ListView>
</LinearLayout>

Tab_2 类:

public class Tab_2 extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);

        // Create Parser for raw/list_combo_offer.xml
        ItemParser itemParser = new ItemParser();
        InputStream inputStream = getResources().openRawResource(
                R.raw.list_tab2);

        // Parse the inputstream
        itemParser.parse(inputStream);

        // Get items
        List<Item> itemList = itemParser.getList();

        // Create a customized ArrayAdapter
        ItemArrayAdapter adapter = new ItemArrayAdapter(
                this, //getApplicationContext(), 
                R.layout.item_list, 
                itemList);

        ListView lv = (ListView) findViewById(android.R.id.list);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new OnItemClickListener()
        {
             @Override
             public void onItemClick(AdapterView<?> a, View v, int position, long id) 
             {
                 Intent intent = new Intent(Tab_2.this, ItemActivity.class);
                 intent.putExtra("position", (int) position);
                 startActivity(intent);
             }
        });

    }
}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tbf.tb.simulador1o1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="13"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="tbf.tb.simulador1o1.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="tbf.tb.simulador1o1.Tab_1"
            android:label="@string/tab_1"
            android:parentActivityName="tbf.tb.simulador1o1.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="tbf.tb.simulador1o1.MainActivity" />
        </activity>

        <activity
            android:name="tbf.tb.simulador1o1.Tab_2"
            android:label="@string/tab_2"
            android:parentActivityName="tbf.tb.simulador1o1.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="tbf.tb.simulador1o1.MainActivity" />
        </activity>

        <activity
            android:name="tbf.tb.simulador1o1.Tab_3"
            android:label="@string/tab_3"
            android:parentActivityName="tbf.tb.simulador1o1.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="tbf.tb.simulador1o1.MainActivity" />
        </activity>

        <activity
            android:name="tbf.tb.simulador1o1.Tab_4"
            android:label="@string/tab_4"
            android:parentActivityName="tbf.tb.simulador1o1.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="tbf.tb.simulador1o1.MainActivity" />
        </activity>

        <activity
            android:name="tbf.tb.simulador1o1.Tab_5"
            android:label="@string/tab_5"
            android:parentActivityName="tbf.tb.simulador1o1.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="tbf.tb.simulador1o1.MainActivity" />
        </activity>

    </application>

</manifest>

【问题讨论】:

    标签: android android-listview android-activity runtime-error android-tabhost


    【解决方案1】:

    修复

    MainActivity 布局:

    <TabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1"/>
    
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:layout_marginBottom="-4dp"/>
    
        </LinearLayout>
    </TabHost>
    

    AndroidManifest:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="tbf.tb.simulador1o1"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="13"
            android:targetSdkVersion="17" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    
            <activity
                android:name="tbf.tb.simulador1o1.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="tbf.tb.simulador1o1.Tab_1"/>
            <activity android:name="tbf.tb.simulador1o1.Tab_2"/>
            <activity android:name="tbf.tb.simulador1o1.Tab_3"/>
            <activity android:name="tbf.tb.simulador1o1.Tab_4"/>
            <activity android:name="tbf.tb.simulador1o1.Tab_5"/>
            <activity android:name="tbf.tb.simulador1o1.ItemActivity"/>
    
        </application>
    
    </manifest>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 2019-10-29
      相关资源
      最近更新 更多