Reputation: 4601
I am trying to implement TabLayout in an Activity, and I keep on getting error for it. My main_new.xml XML file is :
<?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>
My Activity class is :
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);
}
}
Manifest file contains :
<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>
ERROR I get :
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
I use SDK 1.6 and in my manist also have android:minSdkVersion="4" . I have used http://www.codeproject.com/KB/android/AndroidTabs.aspx for Tab tutorial.
Can anyone help me know the error. I have spend hours in this error. Any help is highly appreciated. Please try to help soon.
Thanks
Upvotes: 0
Views: 1086
Reputation: 4601
My 1 mistake in xml file : I had closed Framelayout insteead of closing it after full layout. That was my mistake. I found it and the problem is solved.
Thanks a lot to everyone for viewing it. @yogsma, Thanks for taking efforts to help me out.
Thanks
Upvotes: 0
Reputation: 10586
You don't have any component in your layout with id tabhost
.
Added the code to change here Try this
TabHost tabHost = getTabHost();
in your code instead of
(TabHost)findViewById(R.id.tabHost);
Upvotes: 1