码迷,mamicode.com
首页 > 其他好文 > 详细

ViewPager+FragmentTabhost 解决办法

时间:2015-01-13 14:23:07      阅读:523      评论:0      收藏:0      [点我收藏+]

标签:

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
import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
 
 
//************************自定义pagerAdapter**********************
public class MviewPagerAdapter extends FragmentStatePagerAdapter{
     
    private final ArrayList<TabsInfo> lstTabsInfo;
    private final Context mContext;
    public static class TabsInfo {
        private final Class<?> clss_;
        private final Bundle args_;
        public TabsInfo(Class<?> clss, Bundle args) {
            this.clss_ = clss;
            this.args_ = args;
        }
    }
    public MviewPagerAdapter(FragmentActivity activity, ArrayList<TabsInfo> lstTabsInfo) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        this.lstTabsInfo = lstTabsInfo;
    }
 
    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        TabsInfo info = lstTabsInfo.get(position);
        return Fragment.instantiate(mContext, info.clss_.getName(), info.args_);
    }
 
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return lstTabsInfo.size();
    }
}
 
//*******************应用************************
public class MainActivity extends FragmentActivity implements OnPageChangeListener, OnCheckedChangeListener{
    private RadioGroup mRadioGroup;
    private ViewPager mViewPager;
    private ArrayList<Integer> lstCheckIds;
    private ArrayList<TabsInfo> lstTabsInfos;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRadioGroup = (RadioGroup)findViewById(R.id.radiogroup);
        mRadioGroup.check(R.id.chengzai);
        lstCheckIds = new ArrayList<Integer>();
        for(int i=0; i<5; i++) {
            lstCheckIds.add(R.id.chengzai+i);
        }
        mRadioGroup.setOnCheckedChangeListener(this);
        mViewPager = (ViewPager)findViewById(R.id.main_viewPager);
        lstTabsInfos = new ArrayList<TabsInfo>();
        //**********此处为关键代码*************
        lstTabsInfos.add(new TabsInfo(FireDisasterFragment.class, null));
        lstTabsInfos.add(new TabsInfo(SuperviseFragment.class, null));
        lstTabsInfos.add(new TabsInfo(Tab1.class, null));
        lstTabsInfos.add(new TabsInfo(Tab2.class, null));
        lstTabsInfos.add(new TabsInfo(Tab1.class, null));
        MviewPagerAdapter adapter = new MviewPagerAdapter(this, lstTabsInfos);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(0);
        mViewPager.setOnPageChangeListener(this);
    }
     
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        for(int i=0; i<lstCheckIds.size(); i++) {
            if(lstCheckIds.get(i) == checkedId) {
                mViewPager.setCurrentItem(i);
                break;
            }
        }
    }
     
    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub
         
    }
     
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub
         
    }
     
    @Override
    public void onPageSelected(int position) {
        // TODO Auto-generated method stub
        mRadioGroup.check(lstCheckIds.get(position));
    }
 
}
 
//*********另附上 FireDisasterFragment(FragmentTabhost)关键代码**************
@Override
    public View 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);
        return view;
    }
 
//*********************布局代码***************************
<?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>

ViewPager+FragmentTabhost 解决办法

标签:

原文地址:http://blog.csdn.net/u014311077/article/details/42675857

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!