0

以下代码是我从示例中获得的,但我的应用程序总是崩溃。

package com.example.tabview;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import android.view.Menu;


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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Resources ressources = getResources(); 
        TabHost tabHost = getTabHost(); 

        // Android tab
        Intent intentAndroid = new Intent().setClass(this, AndroidActivity.class);
        TabSpec tabSpecAndroid = tabHost
          .newTabSpec("Android")

          .setContent(intentAndroid);

        // Apple tab
        Intent intentApple = new Intent().setClass(this, AppleActivity.class);
        TabSpec tabSpecApple = tabHost
          .newTabSpec("Apple")

          .setContent(intentApple);

        // Windows tab
        Intent intentWindows = new Intent().setClass(this, WindowsActivity.class);
        TabSpec tabSpecWindows = tabHost
          .newTabSpec("Windows")

          .setContent(intentWindows);


        // add all tabs 
        tabHost.addTab(tabSpecAndroid);
        tabHost.addTab(tabSpecApple);
        tabHost.addTab(tabSpecWindows);

        //set Windows tab as default (zero based)
        tabHost.setCurrentTab(2);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我是android新手,谁能帮我解决这个问题

日志猫

07-01 14:48:25.127: E/AndroidRuntime(869): FATAL EXCEPTION: main
07-01 14:48:25.127: E/AndroidRuntime(869): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabview/com.example.tabview.MainActivity}: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.
07-01 14:48:25.127: E/AndroidRuntime(869):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-01 14:48:25.127: E/AndroidRuntime(869):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-01 14:48:25.127: E/AndroidRuntime(869):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-01 14:48:25.127: E/AndroidRuntime(869):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-01 14:48:25.127: E/AndroidRuntime(869):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 14:48:25.127: E/AndroidRuntime(869):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-01 14:48:25.127: E/AndroidRuntime(869):  at java.lang.reflect.Method.invokeNative(Native Method)
07-01 14:48:25.127: E/AndroidRuntime(869):  at java.lang.reflect.Method.invoke(Method.java:511)
07-01 14:48:25.127: E/AndroidRuntime(869):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-01 14:48:25.127: E/AndroidRuntime(869):  at com.example.tabview.MainActivity.onCreate(MainActivity.java:46)
07-01 14:48:25.127: E/AndroidRuntime(869):  at android.app.Activity.performCreate(Activity.java:5104)
07-01 14:48:25.127: E/AndroidRuntime(869):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
4

1 回答 1

2

如错误所示

java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.

看来你tabIndicator在你的TabSpec

有关使用setIndicator的不同方法,请参阅文档

从文档

对于选项卡指示器,您的选择是:1) 设置标签 2) 设置标签和图标

我不能告诉你你需要什么,但这些链接应该会有所帮助

于 2013-07-01T15:00:56.513 回答