1

我正在学习在 android 中制作基于选项卡的应用程序。在 LogCat 中出现“您必须指定创建选项卡指示器的方法”错误。

这是代码......

.java 文件......

package com.example.tabdemo;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;


@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

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

        TabHost tabhost=getTabHost();

        TabHost.TabSpec spec1=tabhost.newTabSpec("Tab 1");
        Intent intent1=new Intent(this, Tabone.class);
        spec1.setContent(intent1);
       // spec1.setIndicator("Tab 1", getResources().getDrawable(R.drawable.face));
        spec1.setIndicator("Tab 1");
        tabhost.addTab(spec1);

        TabHost.TabSpec spec2=tabhost.newTabSpec("Tab 2");
        Intent intent2=new Intent(this,Tabtwo.class);
        spec2.setContent(intent2);
     //   spec1.setIndicator("Tab 2", getResources().getDrawable(R.drawable.face));
        spec1.setIndicator("Tab 2");


        tabhost.addTab(spec2);
        tabhost.setCurrentTab(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: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>
4

1 回答 1

5

您没有为 tab2 设置指标

检查你的代码

spec2.setContent(intent2);
 //   spec1.setIndicator("Tab 2", getResources().getDrawable(R.drawable.face));
    spec1.setIndicator("Tab 2");

你覆盖你的 tab1 指标。

于 2012-08-22T05:47:13.150 回答