pratnala pratnala - 3 months ago 10
Java Question

Unable to start activity ComponentInfo - java.lang.IllegalStateException

I have the following activity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);

TabHost mainTabHost = (TabHost) findViewById(R.id.mainTabHost);
mainTabHost.setup();

mainTabHost.addTab(mainTabHost.newTabSpec("Tab 1").setIndicator("", ContextCompat.getDrawable(this, R.drawable.d1)).setContent(new Intent(this, Activity1.class)));

mainTabHost.addTab(mainTabHost.newTabSpec("Tab 2").setIndicator("", ContextCompat.getDrawable(this, R.drawable.d2)).setContent(new Intent(this, Activity2.class)));

mainTabHost.addTab(mainTabHost.newTabSpec("Tab 3").setIndicator("", ContextCompat.getDrawable(this, R.drawable.d3)).setContent(new Intent(this, Activity3.class)));
}
}


And this is the XML

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@id/mainTabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activityHorizontalMargin"
android:paddingRight="@dimen/activityHorizontalMargin"
android:paddingTop="@dimen/activityVerticalMargin"
tools:context=".MainActivity">

<RelativeLayout
android:id="@id/mainHomeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />

<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />

</RelativeLayout>

</TabHost>


There is another activity, in which tapping a button launches this activity. However, the activity doesn't launch and I get the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nirvanapass/com.nirvanapass.MainActivity}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?


I have searched online - but whatever solutions I could find use
ActivityGroup
or
TabActivity
- both of which are deprecated. How to tackle this error and solve this problem?

Answer

The best solution is to use a modern tab solution. TabHost has been obsolete for quite some time. Using an Intent for a tab content has been deprecated for five years, and it was never a good idea even before then.

Most modern tab implementations use a ViewPager with some sort of tabbed indicator. PagerTabStrip and TabLayout are both in the Android SDK, and the Android Arsenal lists many other tab indicators in their ViewPager category.

If you really want the retro styling of a TabWidget, either use FragmentTabHost, or use TabHost where the tabs are views.

Here are sample apps for:

Comments