我正在android中制作标签布局。我在这一行收到 RuntimeException 错误tabHost.addTab(spec);
。我在哪里犯错?
仪表板活动
public class DashboardActivity extends TabActivity {
Button btnLogout;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
btnLogout = (Button)findViewById(R.id.btnLogout);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
//Artist Tab
intent = new Intent(this, Artists.class);
spec = tabHost.newTabSpec("artists").setIndicator("Artist", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
tabHost.addTab(spec);
//Songs
intent = new Intent(this, Songs.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
tabHost.addTab(spec);
//Albums
intent = new Intent(this, Album.class);
spec = tabHost.newTabSpec("artists").setIndicator("Artist", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
tabHost.addTab(spec);
//tabHost.setCurrentTab(1);
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(i);
finish();
}
});
}
}
这是我的dashboard.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"
android:orientation="vertical" >
<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" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:gravity="center"
android:text="WELCOME"
android:textSize="40dip" />
<Button
android:id="@+id/btnLogout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dip"
android:background="@null"
android:text="Logout Me"
android:textColor="#21dbd4"
android:textSize="20dip"
android:textStyle="bold" />
</TabHost>
这是我的 logcat 输出
05-29 05:02:12.556: D/dalvikvm(27476): GC_EXTERNAL_ALLOC freed 40K, 50% free 2725K/5379K, external 0K/0K, paused 121ms
05-29 05:02:12.636: D/dalvikvm(27476): GC_EXTERNAL_ALLOC freed 7K, 50% free 2743K/5379K, external 455K/518K, paused 25ms
05-29 05:02:12.781: D/CLIPBOARD(27476): Hide Clipboard dialog at Starting input: finished by someone else... !
05-29 05:02:34.136: D/dalvikvm(27476): GC_CONCURRENT freed 117K, 46% free 3054K/5639K, external 327K/661K, paused 7ms+2ms
05-29 05:02:34.151: D/AndroidRuntime(27476): Shutting down VM
05-29 05:02:34.151: W/dalvikvm(27476): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-29 05:02:34.156: E/AndroidRuntime(27476): FATAL EXCEPTION: main
05-29 05:02:34.156: E/AndroidRuntime(27476): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zafar.login/com.zafar.login.DashboardActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.zafar.login/com.zafar.login.Artists}; have you declared this activity in your AndroidManifest.xml?
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.os.Looper.loop(Looper.java:130)
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.app.ActivityThread.main(ActivityThread.java:3691)
05-29 05:02:34.156: E/AndroidRuntime(27476): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 05:02:34.156: E/AndroidRuntime(27476): at java.lang.reflect.Method.invoke(Method.java:507)
05-29 05:02:34.156: E/AndroidRuntime(27476): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-29 05:02:34.156: E/AndroidRuntime(27476): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-29 05:02:34.156: E/AndroidRuntime(27476): at dalvik.system.NativeStart.main(Native Method)
05-29 05:02:34.156: E/AndroidRuntime(27476): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.zafar.login/com.zafar.login.Artists}; have you declared this activity in your AndroidManifest.xml?
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.app.ActivityThread.resolveActivityInfo(ActivityThread.java:1461)
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:277)
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:691)
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.widget.TabHost.setCurrentTab(TabHost.java:341)
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.widget.TabHost.addTab(TabHost.java:226)
05-29 05:02:34.156: E/AndroidRuntime(27476): at com.zafar.login.DashboardActivity.onCreate(DashboardActivity.java:29)
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-29 05:02:34.156: E/AndroidRuntime(27476): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-29 05:02:34.156: E/AndroidRuntime(27476): ... 11 more
05-29 05:02:40.606: I/Process(27476): Sending signal. PID: 27476 SIG: 9