java - android标签布局问题

标签 java android

我正在 android 中制作选项卡布局。我在这一行遇到 RuntimeException 错误 tabHost.addTab(spec);。我哪里出错了?

仪表板 Activity

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();
        }
    });
}   

}

这是我的仪表板.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

最佳答案

您需要在 AndroidManifest.xml 文件中声明您的 Activity

<activity 
    android:name="com.zafar.login.DashboardActivity" 
    android:label="@string/app_name">
</activity>

关于java - android标签布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10792352/

相关文章:

java - 检测所有小部件的 Android 实例?

android - 将 Activity 的上下文传递给 Android 中的另一个 Activity

java - 变压器从标签中删除了空格

java - 作为名称或标识符的保留字

java - spring 工具套件默认 mvc 项目 Web 应用程序

java - 在 Android 的主屏幕小部件中填充 ListView

android - 如何将使用 SensorListener 的程序转换为 SensorEventListener

android - 为流发布设置 facebook 权限

java - 使用 CharAt() 检查多个字符

java - Android Volley JsonObjectRequest onResponse 如何允许 JSONObject 为 NULL