App Crashing尝试使用TabHost

时间:2014-08-21 12:25:40

标签: android android-tabhost tabwidget

我尝试创建的视图类似于Google + 的个人资料部分,但没有视差。只是一个布局,在其下方的顶部和两个标签处放置3 TextView 。按照说明我做了我认为可能正确的事情。我的代码在我的XML和类文件下面

<?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>

班级文件代码

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);
    }
}

我的日志一直告诉我56号线有问题//tabHost.addTab(tab1);是第56行,我的日志在下面

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

2 个答案:

答案 0 :(得分:1)

您需要将此行添加到代码中

  tabHost.setup(this.getLocalActivityManager());

答案 1 :(得分:0)

您需要将MainActivity的基类从Activity更改为ActivityGroup,如下所示:

public class MainActivity extends ActivityGroup {
  ...
  }

ActivityGroup将处理LocalActivityManager的一个实例。所以你不需要创建它。更改基类后,只需调用基类中定义的getLocalActivityManager()函数即可获取该实例。调用tabHost的设置功能如下:

tabHost.setup(this.getLocalActivityManager());