App crashes on setContent(new Intent ) in tabhost

179 views Asked by At

I'm using Android's Tabhost and can't view my A activity in Main Activity. It throws error in setIndicatorsetContent(new Intent(this, a.class)). What changes are required? Do I need to change some code in A activity?


MainActivity code

public class MainActivity extends AppCompatActivity {

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

    TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
    tabHost.setup();

    //tab 1
    TabHost.TabSpec spec  = tabHost.newTabSpec("Tab 1");
    spec.setContent(new Intent(this,a.class));
    spec.setIndicator("Exams");
    tabHost.addTab(spec);

    //tab 2
    spec = tabHost.newTabSpec("Tab 2");
    spec.setContent(new Intent(this,b.class));
    spec.setIndicator("Pratice");
    tabHost.addTab(spec);


    }
}

Error Log

Process: com.example.vivek.helloworld, PID: 17776
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vivek.helloworld/com.example.vivek.helloworld.MainActivity}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
  Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:722)
    at android.widget.TabHost.setCurrentTab(TabHost.java:388)
    at android.widget.TabHost.addTab(TabHost.java:222)
    at com.example.vivek.helloworld.MainActivity.onCreate(MainActivity.java:22)
    at android.app.Activity.performCreate(Activity.java:6662)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
1

There are 1 answers

3
Nero On

As it's stated within the error: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
Your need to state the localactivitymanager in order to resolve this error. (FYI - IllegalStateException occurs when you've invoked the method at an illegal or inappropriate time)

Therefore, you need to inform the activity about the state and in order to do so, implement the following code:

tabHost.setup(this.getLocalActivityManager());

I also want to bring to your attention that TabHost has been deprecated since API Level 13. The replacement for TabHost is Fragment and Fragment Manager. It's good practice to ensure you are not implementing outdated/deprecated classes.

This class was deprecated in API level 13.
Use the new Fragment and FragmentManager APIs instead; these are also available on older platforms through the Android compatibility package.

LocalActivityManager