android:使用Tab视图和动态片段

时间:2013-09-19 14:26:57

标签: android android-fragments android-tabhost

我创建了一个包含选项卡视图和动态片段概念的应用程序。当我在按下标签时切换到不同的活动时,代码早先正常工作,但是当我将其转换为片段时,dushhhh。我知道处理片段和活动是两个不同的事情,但逻辑是相同的只有少数这里有变化,这是代码。

The main layout is  "activity_main.xml" 

    <TabHost 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />

            <FrameLayout
                android:id="@+id/tabFrameLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>

    </TabHost>

当用户按下任何标签时,我使用了两个片段进行切换:     其中一个片段布局是“googleMap.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" >

        <TextView android:id="@+id/google_map"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="MAP"/>

    </LinearLayout>

第二个片段是:“friends_list.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" >

        <TextView
            android:id="@+id/textViewFriends"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="friends list will appear here" />

        <ListView
            android:id="@+id/friends_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

    </LinearLayout>

主要活动是:“MainActivity.java”:

    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentTabHost;

    import com.example.friendsfinder.R;

    public class MainActivity extends FragmentActivity {
        private FragmentTabHost mTabHost;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);
            setTabContent();

        }

        private void setTabContent() {
            mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
            mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);


            mTabHost.addTab(
                    mTabHost.newTabSpec("map").setIndicator("Map",
                            getResources().getDrawable(android.R.drawable.star_on)),
                    MapFragment.class, null);
            mTabHost.addTab(
                    mTabHost.newTabSpec("friends list").setIndicator("Friends List",
                            getResources().getDrawable(android.R.drawable.star_on)),
                    FriendsListFragment.class, null);


        }
    }

第一个片段文件是“FriendsListFragment.java”:

    import com.example.friendsfinder.R;

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class FriendsListFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View view=inflater.inflate(R.layout.friends_list, container,false);


            return view;
        }

    }

第二个片段文件是:“MapFragment.java”:

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class MapFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.google_map, container,false);

            return view;

        }

    }

即将发生的错误是:Logcat条目是:

09-19 19:26:40.767: E/AndroidRuntime(28679): FATAL EXCEPTION: main
09-19 19:26:40.767: E/AndroidRuntime(28679): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.friendsfinder/com.webonise.friendsfinder.MainActivity}: java.lang.ClassCastException: android.widget.TabHost
09-19 19:26:40.767: E/AndroidRuntime(28679):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-19 19:26:40.767: E/AndroidRuntime(28679):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-19 19:26:40.767: E/AndroidRuntime(28679):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-19 19:26:40.767: E/AndroidRuntime(28679):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-19 19:26:40.767: E/AndroidRuntime(28679):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 19:26:40.767: E/AndroidRuntime(28679):    at android.os.Looper.loop(Looper.java:130)
09-19 19:26:40.767: E/AndroidRuntime(28679):    at android.app.ActivityThread.main(ActivityThread.java:3683)
09-19 19:26:40.767: E/AndroidRuntime(28679):    at java.lang.reflect.Method.invokeNative(Native Method)
09-19 19:26:40.767: E/AndroidRuntime(28679):    at java.lang.reflect.Method.invoke(Method.java:507)
09-19 19:26:40.767: E/AndroidRuntime(28679):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-19 19:26:40.767: E/AndroidRuntime(28679):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-19 19:26:40.767: E/AndroidRuntime(28679):    at dalvik.system.NativeStart.main(Native Method)
09-19 19:26:40.767: E/AndroidRuntime(28679): Caused by: java.lang.ClassCastException: android.widget.TabHost
09-19 19:26:40.767: E/AndroidRuntime(28679):    at com.webonise.friendsfinder.MainActivity.setTabContent(MainActivity.java:22)
09-19 19:26:40.767: E/AndroidRuntime(28679):    at com.webonise.friendsfinder.MainActivity.onCreate(MainActivity.java:17)
09-19 19:26:40.767: E/AndroidRuntime(28679):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-19 19:26:40.767: E/AndroidRuntime(28679):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-19 19:26:40.767: E/AndroidRuntime(28679):    ... 11 more
09-19 19:26:51.617: I/Process(28679): Sending signal. PID: 28679 SIG: 9

2 个答案:

答案 0 :(得分:2)

您正在尝试将TabHost强制转换为FragmentTabHost

试试这个

<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />

        <FrameLayout
            android:id="@+id/tabFrameLayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

</android.support.v4.app.FragmentTabHost >

答案 1 :(得分:1)

您的主要活动

中有一个类强制转换异常
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

findviewbyid(android.R.id.tabhost); android.R.id.tabhost ????检查此行,您将不会收到此错误。