我正在尝试在Activity中实现TabLayout,并且我一直在为它获取错误。 我的main_new.xml XML文件是:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/tabHost">
<TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" />
<FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" />
<LinearLayout android:id="@+id/tab1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingTop="50dp">
<TextView android:layout_width="fill_parent"
android:layout_height="100px"
android:text="This is tab1"
android:id="@+id/txt1"/>
</LinearLayout>
<LinearLayout android:id="@+id/tab2" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingTop="50dp">
<TextView android:layout_width="fill_parent"
android:layout_height="100px"
android:text="This is tab2"
android:id="@+id/txt2"/>
</LinearLayout>
</TabHost>
我的活动课程是:
public class UltimateActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_new);
TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1 = tabHost.newTabSpec("Tab 1");
spec1.setContent(R.id.tab1); // Here iis where the errror points
spec1.setIndicator("Tab 1");
TabSpec spec2 = tabHost.newTabSpec("Info");
spec2.setContent(R.id.tab2);
spec2.setIndicator("Info");
tabHost.addTab(spec1);
tabHost.addTab(spec2);
}
}
清单文件包含:
<activity android:icon="@drawable/icon" android:label="Ultimate" android:name="UltimateActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我得到的错误:
05-18 22:13:56.072: ERROR/AndroidRuntime(634): Uncaught handler: thread main exiting due to uncaught exception
05-18 22:13:56.083: ERROR/AndroidRuntime(634): java.lang.RuntimeException: Unable to start activity ComponentInfo{orange.android.vpn/orange.android.vpn.UltimateActivity}: java.lang.RuntimeException: Could not create tab content because could not find view with id 2131296290
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at android.os.Looper.loop(Looper.java:123)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at java.lang.reflect.Method.invoke(Method.java:521)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at dalvik.system.NativeStart.main(Native Method)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): Caused by: java.lang.RuntimeException: Could not create tab content because could not find view with id 2131296290
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:587)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:578)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at android.widget.TabHost$TabSpec.setContent(TabHost.java:435)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at orange.android.vpn.UltimateActivity.onCreate(UltimateActivity.java:20)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-18 22:13:56.083: ERROR/AndroidRuntime(634): ... 11 more
我使用SDK 1.6并且在我的manist中也有android:minSdkVersion =“4”。我已经使用http://www.codeproject.com/KB/android/AndroidTabs.aspx作为Tab教程。
任何人都可以帮我知道错误。我在这个错误中花了好几个小时。任何帮助都非常感谢。请尽快帮忙。
由于
答案 0 :(得分:1)
您的布局中没有任何ID为tabhost
的组件。
在此处添加了要更改的代码 试试这个
TabHost tabHost = getTabHost();
在代码中而不是
(TabHost)findViewById(R.id.tabHost);
答案 1 :(得分:0)
我在xml文件中的1个错误: 完全布局后,我关闭了Framelayout insteead关闭它。 那是我的错。我找到了,问题就解决了。
非常感谢大家查看它。 @yogsma,感谢您努力帮助我。
由于