机器人:使用动态片段选项卡中查看 [英] android : using Tab view with dynamic fragment

查看:236
本文介绍了机器人:使用动态片段选项卡中查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了包含标签视图和动态片段概念的应用程序。在code的早期工作正常时,当我切换到不同的活动上pressing的标签,但是当我把它转换为片段工作,dushhhh.I知道碎片的工作和活动是两个不同的东西,但道理是一样的只是一些改变这里有,这里是code。

 主要布局为activity_main.xml中    < TabHost
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@机器人:ID / tabhost
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent>       <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:方向=垂直>            < TabWidget
                机器人:ID =@机器人:ID /标签
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_weight =0
                机器人:方向=横向/>            <的FrameLayout
                机器人:ID =@ + ID / tabFrameLayout
                机器人:layout_width =match_parent
                机器人:layout_height =0dp
                机器人:layout_weight =1/>
        < / LinearLayout中>    < / TabHost>

我用两个片段,当用户presses任何选项卡的切换通过:
    片段布局中的一个是googleMap.xml:

 <?XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>        < TextView的机器人:ID =@ + ID / google_map
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =match_parent
            机器人:文字=地图/>    < / LinearLayout中>

第二个片段是:friends_list.xml

 <?XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>        <的TextView
            机器人:ID =@ + ID / textViewFriends
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=好友列表会出现在这里/>        < ListView控件
            机器人:ID =@ + ID / friends_list
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT>
        < /&的ListView GT;    < / LinearLayout中>

主要活动是:MainActivity.java

 进口android.os.Bundle;
    进口android.support.v4.app.FragmentActivity;
    进口android.support.v4.app.FragmentTabHost;    进口com.example.friendsfinder.R;    公共类MainActivity扩展FragmentActivity {
        私人FragmentTabHost mTab​​Host;        @覆盖
        保护无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);            的setContentView(R.layout.activity_main);
            setTabContent();        }        私人无效setTabContent(){
            mTabHost =(FragmentTabHost)findViewById(android.R.id.tabhost);
            mTabHost.setup(这一点,getSupportFragmentManager(),R.id.tabFrameLayout);
            mTabHost.addTab(
                    mTabHost.newTabSpec(图)。setIndicator(地图,
                            getResources()。getDrawable(android.R.drawable.star_on))
                    MapFragment.class,NULL);
            mTabHost.addTab(
                    mTabHost.newTabSpec(好友列表)。setIndicator(好友列表,
                            getResources()。getDrawable(android.R.drawable.star_on))
                    FriendsListFragment.class,NULL);
        }
    }

第一个片段文件是FriendsListFragment.java

 进口com.example.friendsfinder.R;    进口android.os.Bundle;
    进口android.support.v4.app.Fragment;
    进口android.view.LayoutInflater;
    进口android.view.View;
    进口android.view.ViewGroup;    公共类FriendsListFragment扩展片段{        @覆盖
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                捆绑savedInstanceState){            查看查看= inflater.inflate(R.layout.friends_list,集装箱,FALSE);
            返回视图。
        }    }

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

 进口android.os.Bundle;
    进口android.support.v4.app.Fragment;
    进口android.view.LayoutInflater;
    进口android.view.View;
    进口android.view.ViewGroup;    公共类MapFragment扩展片段{
        @覆盖
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                捆绑savedInstanceState){
            查看查看= inflater.inflate(R.layout.google_map,集装箱,FALSE);            返回视图。        }    }

这即将到来的错误是这样的:logcat的条目是:

  09-19 19:26:40.767:E / AndroidRuntime(28679):致命异常:主要
09-19 19:26:40.767:E / AndroidRuntime(28679):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.friendsfinder / com.webonise.friendsfinder.MainActivity}:java.lang.ClassCastException:机器人.widget.TabHost
09-19 19:26:40.767:E / AndroidRuntime(28679):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-19 19:26:40.767:E / AndroidRuntime(28679):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-19 19:26:40.767:E / AndroidRuntime(28679):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
09-19 19:26:40.767:E / AndroidRuntime(28679):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
09-19 19:26:40.767:E / AndroidRuntime(28679):在android.os.Handler.dispatchMessage(Handler.java:99)
09-19 19:26:40.767:E / AndroidRuntime(28679):在android.os.Looper.loop(Looper.java:130)
09-19 19:26:40.767:E / AndroidRuntime(28679):在android.app.ActivityThread.main(ActivityThread.java:3683)
09-19 19:26:40.767:E / AndroidRuntime(28679):在java.lang.reflect.Method.invokeNative(本机方法)
09-19 19:26:40.767:E / AndroidRuntime(28679):在java.lang.reflect.Method.invoke(Method.java:507)
09-19 19:26:40.767:E / AndroidRuntime(28679):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
09-19 19:26:40.767:E / AndroidRuntime(28679):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-19 19:26:40.767:E / AndroidRuntime(28679):在dalvik.system.NativeStart.main(本机方法)
09-19 19:26:40.767:E / AndroidRuntime(28679):java.lang.ClassCastException:产生的原因android.widget.TabHost
09-19 19:26:40.767:E / AndroidRuntime(28679):在com.webonise.friendsfinder.MainActivity.setTabContent(MainActivity.java:22)
09-19 19:26:40.767:E / AndroidRuntime(28679):在com.webonise.friendsfinder.MainActivity.onCreate(MainActivity.java:17)
09-19 19:26:40.767:E / AndroidRuntime(28679):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-19 19:26:40.767:E / AndroidRuntime(28679):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-19 19:26:40.767:E / AndroidRuntime(28679):... 11个
09-19 19:26:51.617:I /流程(28679):发送信号。 PID:28679 SIG:9


解决方案

您正试图投TabHost到FragmentTabHost

试试这个

 < android.support.v4.app.FragmentTabHost
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@机器人:ID / tabhost
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>   <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>        < TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =0
            机器人:方向=横向/>        <的FrameLayout
            机器人:ID =@ + ID / tabFrameLayout
            机器人:layout_width =match_parent
            机器人:layout_height =0dp
            机器人:layout_weight =1/>
    < / LinearLayout中>< /android.support.v4.app.FragmentTabHost>

I have created an application which contains tab view and dynamic fragments concepts. the code was earlier working fine when when i was switching to different activity on pressing the tabs , but when i converted it to work for fragments, dushhhh.I know working on fragments and activity is two different things , but logic is the same just few changes here there , Here is the code .

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>

I have used two fragments to switch through when user presses any of the tab : one of the fragments layout is "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>

the second fragment is : "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>

The main activity is : "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);


        }
    }

The first fragment file is "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;
        }

    }

Second fragment file is : "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;

        }

    }

The error that is coming is this : Logcat entries are :

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

解决方案

You are trying to cast a TabHost to FragmentTabHost

try this

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

这篇关于机器人:使用动态片段选项卡中查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆