ViewPager+FragmentTabhost 解决办法

来源:互联网 发布:java ee 新闻策划案例 编辑:程序博客网 时间:2024/06/01 08:48
android viewpager+fragmentTabhost:直接将一个FragmentTabhost 加入到viewPager中,滑动viewPager再次返回之前FragmentTabhost
会出现java.lang.NullPointerException, 是在FragmentState.instantiate 出错。

第一次加载viewPager不会出错,第二次就会出现以上情况,解决办法如下代码:
注:TabsInfo是关键,在getItem方法中需要使用Fragment.instantiate得到一个Fragment

经测试ViewPager + fragment 不会出现以上所说情况,可以在构造的时候传递一个

ArrayList<Fragmet> list,再通过getItem 返回即可,有不正确的地方还请指正...

[1].[图片] 0815_16_24_01.png 跳至 [1] [2]

[2].[代码] [Java]代码 跳至 [1] [2]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
importjava.util.ArrayList;
importandroid.content.Context;
importandroid.os.Bundle;
importandroid.support.v4.app.Fragment;
importandroid.support.v4.app.FragmentActivity;
importandroid.support.v4.app.FragmentStatePagerAdapter;
 
 
//************************自定义pagerAdapter**********************
publicclass MviewPagerAdapter extendsFragmentStatePagerAdapter{
     
    privatefinal ArrayList<TabsInfo> lstTabsInfo;
    privatefinal Context mContext;
    publicstatic class TabsInfo {
        privatefinal Class<?> clss_;
        privatefinal Bundle args_;
        publicTabsInfo(Class<?> clss, Bundle args) {
            this.clss_ = clss;
            this.args_ = args;
        }
    }
    publicMviewPagerAdapter(FragmentActivity activity, ArrayList<TabsInfo> lstTabsInfo) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        this.lstTabsInfo = lstTabsInfo;
    }
 
    @Override
    publicFragment getItem(intposition) {
        // TODO Auto-generated method stub
        TabsInfo info = lstTabsInfo.get(position);
        returnFragment.instantiate(mContext, info.clss_.getName(), info.args_);
    }
 
    @Override
    publicint getCount() {
        // TODO Auto-generated method stub
        returnlstTabsInfo.size();
    }
}
 
//*******************应用************************
publicclass MainActivity extendsFragmentActivity implementsOnPageChangeListener, OnCheckedChangeListener{
    privateRadioGroup mRadioGroup;
    privateViewPager mViewPager;
    privateArrayList<Integer> lstCheckIds;
    privateArrayList<TabsInfo> lstTabsInfos;
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRadioGroup = (RadioGroup)findViewById(R.id.radiogroup);
        mRadioGroup.check(R.id.chengzai);
        lstCheckIds = newArrayList<Integer>();
        for(inti=0; i<5; i++) {
            lstCheckIds.add(R.id.chengzai+i);
        }
        mRadioGroup.setOnCheckedChangeListener(this);
        mViewPager = (ViewPager)findViewById(R.id.main_viewPager);
        lstTabsInfos = newArrayList<TabsInfo>();
        //**********此处为关键代码*************
        lstTabsInfos.add(newTabsInfo(FireDisasterFragment.class,null));
        lstTabsInfos.add(newTabsInfo(SuperviseFragment.class,null));
        lstTabsInfos.add(newTabsInfo(Tab1.class,null));
        lstTabsInfos.add(newTabsInfo(Tab2.class,null));
        lstTabsInfos.add(newTabsInfo(Tab1.class,null));
        MviewPagerAdapter adapter = newMviewPagerAdapter(this, lstTabsInfos);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(0);
        mViewPager.setOnPageChangeListener(this);
    }
     
    @Override
    publicvoid onCheckedChanged(RadioGroup group, intcheckedId) {
        // TODO Auto-generated method stub
        for(inti=0; i<lstCheckIds.size(); i++) {
            if(lstCheckIds.get(i) == checkedId) {
                mViewPager.setCurrentItem(i);
                break;
            }
        }
    }
     
    @Override
    publicvoid onPageScrollStateChanged(intarg0) {
        // TODO Auto-generated method stub
         
    }
     
    @Override
    publicvoid onPageScrolled(intarg0, floatarg1, intarg2) {
        // TODO Auto-generated method stub
         
    }
     
    @Override
    publicvoid onPageSelected(intposition) {
        // TODO Auto-generated method stub
        mRadioGroup.check(lstCheckIds.get(position));
    }
 
}
 
//*********另附上 FireDisasterFragment(FragmentTabhost)关键代码**************
@Override
    publicView onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Log.e("fire_oncreateview","oncreateview");
         
        View view = (View)inflater.inflate(R.layout.firedisaste_layout, container, false);
        mTabHost = (FragmentTabHost)view.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
        View indicator = (View)inflater.inflate(R.layout.tabwidgets_layout, null);
        TextView textView = (TextView)indicator.findViewById(R.id.tabwidget_indicator);
         
        textView.setText("tabwidget1");
        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(indicator), Tab3.class,null);
         
        View indicator1 = (View)inflater.inflate(R.layout.tabwidgets_layout, null);
        TextView textView2 = (TextView)indicator1.findViewById(R.id.tabwidget_indicator);
        textView2.setText("tabwidget2");
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(indicator1), Tab4.class,null);
        returnview;
    }
 
//*********************布局代码***************************
<?xml version="1.0"encoding="utf-8"?>
<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:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>
 
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>
 
        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
 
    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 房间里进老鼠了怎么办 衣服上有老鼠屎怎么办 如果被老鼠咬了怎么办 儿童被老鼠咬了怎么办 蟑螂爬到衣柜里怎么办 床垫里有老鼠屎怎么办 汽车里进了老鼠怎么办 狗狗感染蜱虫怎么办 脸上有螨虫怎么办才能去除 老鼠被剪丁丁后怎么办 1楼下水道钻老鼠怎么办 月经来了奶水少了怎么办 孕37周霉菌严重怎么办 家里进了飞蚂蚁怎么办 家里进了大蜘蛛怎么办 衣服上有蟑螂卵怎么办 被子上有蟑螂卵怎么办 厨房里的小飞虫怎么办 水果生的小飞虫怎么办 家里的厕所有虫怎么办 人吃了蟑螂药怎么办 静电贴粘不住了怎么办 会飞的蚂蚁咬了怎么办 家里有白蚁怎么办能除根 华为畅享5s黑屏怎么办 家里有个蚂蚁窝怎么办 宿舍有老鼠爬床怎么办 楼上的狗叫扰民怎么办 楼下的狗一直叫怎么办 焊机通电没反应怎么办 输了3万块钱怎么办 行吊遥控器掉了怎么办 狗狗耳朵尖结痂怎么办 蚊帐里进了蚊子怎么办 孕妇用了点蚊香怎么办 孕妇用了电蚊香怎么办 房子里用蟑螂药怎么办 永恒之塔卡住了怎么办 银行口令卡丢了怎么办 面膜过敏脸肿了怎么办 洛奇英雄传fps低怎么办