E / AndroidRuntime(1899):java.lang.RuntimeException:无法启动活动

时间:2015-02-01 13:40:03

标签: android

我尝试做一个简单的程序,我在其中加载带有标签的应用程序。但我有一些我不明白的错误。如果你能帮助我,那将是很棒的。

提前谢谢。

错误:

02-01 14:03:57.204: E/AndroidRuntime(1899): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.customtabs/com.example.customtabs.CustomtabsActivity}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
02-01 14:03:57.204: E/AndroidRuntime(1899):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at android.os.Looper.loop(Looper.java:135)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at android.app.ActivityThread.main(ActivityThread.java:5221)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at java.lang.reflect.Method.invoke(Native Method)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at java.lang.reflect.Method.invoke(Method.java:372)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-01 14:03:57.204: E/AndroidRuntime(1899): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
02-01 14:03:57.204: E/AndroidRuntime(1899):     at android.app.TabActivity.onContentChanged(TabActivity.java:131)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:381)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at android.app.Activity.setContentView(Activity.java:2144)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at com.example.customtabs.CustomtabsActivity.onCreate(CustomtabsActivity.java:14)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at android.app.Activity.performCreate(Activity.java:5933)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-01 14:03:57.204: E/AndroidRuntime(1899):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-01 14:03:57.204: E/AndroidRuntime(1899):     ... 10 more

主文件     CustomtabsActivity.java:

package com.example.customtabs;

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

public class CustomtabsActivity extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customtabs);

        Resources res = getResources(); 
        TabHost tabHost = getTabHost(); 
        TabHost.TabSpec spec; 
        Intent intent; 

        intent = new Intent().setClass(this, Tab1.class);
        spec = tabHost.newTabSpec("Widget").setIndicator("DateTime", res.getDrawable(android.R.drawable.ic_menu_today)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Tab2.class);
        spec = tabHost.newTabSpec("Form").setIndicator("Form", res.getDrawable(android.R.drawable.ic_menu_manage)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Tab3.class);
        spec = tabHost.newTabSpec("onglet3").setIndicator("onglet3", res.getDrawable(android.R.drawable.ic_menu_mapmode)).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
    }

}

Tab1.java:

package com.example.customtabs;

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



public class Tab1 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.onglet1);
    }
}a

Tab2.java和Tab3.java几乎与Tab1.java相同。

1 个答案:

答案 0 :(得分:0)

根本原因是:

Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'

请确保您的布局具有以下内容:

<TabHost android:id="*@android:id/tabhost"