我正在尝试在我的 TabHost 上为 Specs 设置不同的类,但我只有一些错误(粘贴在下面的日志)如果我champ.setIndicator("Champion").setContent(R.id.tab1);
为其他选项卡更改我的等等,它就可以工作。有人可以帮助我吗?我的代码:
TabsMain.java
package com.girardi.lolguides;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabsMain extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs_main);
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
TabSpec champ = tabHost.newTabSpec("champ");
TabSpec equip = tabHost.newTabSpec("item");
champ.setIndicator("Champion").setContent(new Intent(this, Champion.class));
equip.setIndicator("Items").setContent(new Intent(this, Equip.class));
tabHost.addTab(champ);
tabHost.addTab(equip);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tabs_main, menu);
return true;
}
}
冠军.java
package com.girardi.lolguides;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Champion extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* First Tab Content */
TextView textView = new TextView(this);
textView.setText("First Tab");
setContentView(textView);
}
}
装备.java
package com.girardi.lolguides;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Equip extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Second Tab Content */
TextView textView = new TextView(this);
textView.setText("Second Tab");
setContentView(textView);
}
}
activity_tabs_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TabsMain" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
日志:
05-20 13:51:22.890: D/AndroidRuntime(22816): Shutting down VM
05-20 13:51:22.890: W/dalvikvm(22816): threadid=1: thread exiting with uncaught exception (group=0x40e832a0)
05-20 13:51:22.910: E/AndroidRuntime(22816): FATAL EXCEPTION: main
05-20 13:51:22.910: E/AndroidRuntime(22816): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.girardi.lolguides/com.girardi.lolguides.TabsMain}: java.lang.NullPointerException
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.os.Looper.loop(Looper.java:137)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.main(ActivityThread.java:4898)
05-20 13:51:22.910: E/AndroidRuntime(22816): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 13:51:22.910: E/AndroidRuntime(22816): at java.lang.reflect.Method.invoke(Method.java:511)
05-20 13:51:22.910: E/AndroidRuntime(22816): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-20 13:51:22.910: E/AndroidRuntime(22816): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-20 13:51:22.910: E/AndroidRuntime(22816): at dalvik.system.NativeStart.main(Native Method)
05-20 13:51:22.910: E/AndroidRuntime(22816): Caused by: java.lang.NullPointerException
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.widget.TabHost.addTab(TabHost.java:243)
05-20 13:51:22.910: E/AndroidRuntime(22816): at com.girardi.lolguides.TabsMain.onCreate(TabsMain.java:23)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.Activity.performCreate(Activity.java:5206)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
05-20 13:51:22.910: E/AndroidRuntime(22816): ... 11 more
05-20 13:53:10.655: D/AndroidRuntime(24134): Shutting down VM
05-20 13:53:10.655: W/dalvikvm(24134): threadid=1: thread exiting with uncaught exception (group=0x40e832a0)
05-20 13:53:10.665: E/AndroidRuntime(24134): FATAL EXCEPTION: main
05-20 13:53:10.665: E/AndroidRuntime(24134): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.girardi.lolguides/com.girardi.lolguides.TabsMain}: java.lang.NullPointerException
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.os.Looper.loop(Looper.java:137)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.main(ActivityThread.java:4898)
05-20 13:53:10.665: E/AndroidRuntime(24134): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 13:53:10.665: E/AndroidRuntime(24134): at java.lang.reflect.Method.invoke(Method.java:511)
05-20 13:53:10.665: E/AndroidRuntime(24134): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-20 13:53:10.665: E/AndroidRuntime(24134): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-20 13:53:10.665: E/AndroidRuntime(24134): at dalvik.system.NativeStart.main(Native Method)
05-20 13:53:10.665: E/AndroidRuntime(24134): Caused by: java.lang.NullPointerException
05-20 13:53:10.665: E/AndroidRuntime(24134): at com.girardi.lolguides.TabsMain.onCreate(TabsMain.java:19)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.Activity.performCreate(Activity.java:5206)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
05-20 13:53:10.665: E/AndroidRuntime(24134): ... 11 more