user3889764
user3889764

Reputation:

App Crashing trying to use TabHost

I'm trying to create a view that looks like the profile section of Google+ but not with the Parallax. Just a Layout holding 3 TextView at the Top and Two Tabs below it. I did what I think might be right by following Instructions. My Code's below both my XML and Class file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/abc_ab_bottom_solid_light_holo" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15dp"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="TextView" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="TextView" />
        </LinearLayout>

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:src="@drawable/abc_ab_bottom_solid_light_holo" />
    </LinearLayout>

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="18dp"
        android:background="@android:color/darker_gray" />

    <TabHost
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
          android:id="@android:id/tabhost">

        <LinearLayout 
                android:id="@+id/LinearLayout01"
                android:orientation="vertical" 
                android:layout_height="fill_parent"
                android:layout_width="fill_parent">

                <TabWidget 
                    android:id="@android:id/tabs"
                    android:layout_height="wrap_content" 
                    android:layout_width="fill_parent">
                </TabWidget>

                <FrameLayout 
                    android:id="@android:id/tabcontent"
                    android:layout_height="fill_parent"
                     android:layout_width="fill_parent">
                </FrameLayout>

        </LinearLayout>

</TabHost>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

Class file code

public class OrderActivity extends Activity{

    TextView txt1, txt2, txt3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_id_layout);

        // create the TabHost that will contain the Tabs
        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
        TabSpec tab1 = tabHost.newTabSpec("DETAILS");
        TabSpec tab2 = tabHost.newTabSpec("ISSUES");
        tabHost.setup();
        final TabWidget tabWidget = tabHost.getTabWidget();
        FrameLayout tabContent = tabHost.getTabContentView();


        txt1 = (TextView) findViewById(R.id.textView1);
        txt2 = (TextView) findViewById(R.id.textView2);
        txt3 = (TextView) findViewById(R.id.textView3);

        Bundle extras = getIntent().getExtras();
        txt1.setText(extras.getString("key1"));
        txt2.setText(extras.getString("key2"));
        txt3.setText(extras.getString("key3"));

        /*
         * Set the Tab name and Activity
         * that will be opened when particular Tab is selected
         */
        tab1.setIndicator("DETAILS");
        tab1.setContent(new Intent(this, DetailsActivity.class));

        tab2.setIndicator("ISSUES");
        tab2.setContent(new Intent(this, IssuesActivity.class));

        /*
         * Add tabs to tabHost for it to display
         */
        tabHost.addTab(tab1);
        tabHost.addTab(tab2);
    }
}

And my Log keeps telling me Line 56 is at fault //tabHost.addTab(tab1); is Line 56, My Log is below

08-21 13:01:36.871: E/AndroidRuntime(22972): FATAL EXCEPTION: main
08-21 13:01:36.871: E/AndroidRuntime(22972): Process: com.deliveryscience.track, PID: 22972
08-21 13:01:36.871: E/AndroidRuntime(22972): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.deliveryscience.track/com.deliveryscience.track.OrderActivity}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
08-21 13:01:36.871: E/AndroidRuntime(22972):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at android.app.ActivityThread.access$800(ActivityThread.java)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at android.os.Handler.dispatchMessage(Handler.java)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at android.os.Looper.loop(Looper.java)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at android.app.ActivityThread.main(ActivityThread.java)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at java.lang.reflect.Method.invokeNative(Native Method)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at java.lang.reflect.Method.invoke(Method.java:515)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at dalvik.system.NativeStart.main(Native Method)
08-21 13:01:36.871: E/AndroidRuntime(22972): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
08-21 13:01:36.871: E/AndroidRuntime(22972):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at android.widget.TabHost.setCurrentTab(TabHost.java)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at android.widget.TabHost.addTab(TabHost.java)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at com.deliveryscience.track.OrderActivity.onCreate(OrderActivity.java:56)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at android.app.Activity.performCreate(Activity.java)
08-21 13:01:36.871: E/AndroidRuntime(22972):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
08-21 13:01:36.871: E/AndroidRuntime(22972):    ... 12 more

Upvotes: 0

Views: 130

Answers (2)

vishal arote
vishal arote

Reputation: 126

you need change MainActivity's base class from Activity to ActivityGroup, as follows:

public class MainActivity extends ActivityGroup {
  ...
  }

ActivityGroup will take care of an instance of LocalActivityManager. So you don't need to create it. After the base class is changed, just call getLocalActivityManager() function defined in the base class to get that instance. Call tabHost's setup function like this:

tabHost.setup(this.getLocalActivityManager());

Upvotes: 0

droidchef
droidchef

Reputation: 2307

You need to add this line to your code

  tabHost.setup(this.getLocalActivityManager());

Upvotes: 1

Related Questions