gpt4 book ai didi

Android ViewPager 获取 View 空指针异常

转载 作者:行者123 更新时间:2023-11-29 21:19:44 26 4
gpt4 key购买 nike

我有 support.v4 ViewPager,我使用 FragmentStatePagerAdapter 填充了 fragment 。

ViewPager 中 fragment 的显示按预期工作,但问题是在调用 FragmentActvitiy 类中的 getView() 函数时获取 fragment View 。我可以访问所有 15 个 fragment 并获取参数等,但是在 fragment 上调用 ​​getView() 函数时我得到一个空指针异常,因为第三个 fragment getView() 是 null.

MyPageAdapter adapter = (MyPageAdapter) pager.getAdapter();
Fragment fragment;

for (int i=0; i<15; i++) {
fragment = adapter.getItem(i);
View v = fragment.getView();
System.out.println("--- FRAGMENT = " + fragment + " VIEW = " + v);
}

输出:

--- FRAGMENT = MyPrvaPomocFragment{40ede4c8 #0 id=0x7f06000d}   VIEW = android.support.v4.app.NoSaveStateFrameLayout{40f11cb0 V.E..... ......I. 0,0-540,850}
--- FRAGMENT = MyPrvaPomocFragment{40edf7c0 #1 id=0x7f06000d} VIEW = android.support.v4.app.NoSaveStateFrameLayout{40f16f80 V.E..... ......ID 540,0-1080,850}
--- FRAGMENT = MyPrvaPomocFragment{40ee0998} VIEW = null

FragmentStatePageAdapter类代码:

public class MyPageAdapter extends FragmentStatePagerAdapter {

private List<Fragment> fragments;

public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}

@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}

@Override
public int getCount() {
return this.fragments.size();
}

@Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
}

fragment 类代码:

public class MyPrvaPomocFragment extends Fragment {

public static final String EXTRA_QUESTION_NUMBER = "EXTRA_QUESTION_NUMBER";
public static final String EXTRA_QUESTION = "EXTRA_QUESTION";
...

public static final MyPrvaPomocFragment newInstance(String questionNumber, String question, ..., int correctAnswer) {
MyPrvaPomocFragment f = new MyPrvaPomocFragment();

Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_QUESTION_NUMBER, questionNumber);
bdl.putString(EXTRA_QUESTION, question);
bdl.putInt(EXTRA_CORRECT_ANSWER, correctAnswer);
...
f.setArguments(bdl);

return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String question = getArguments().getString(EXTRA_QUESTION);
String firstAnswer = getArguments().getString(EXTRA_FIRST_ANSWER);
...

View v = inflater.inflate(R.layout.fragment_prva_pomoc, container, false);

TextView textViewQuestion = (TextView) v.findViewById(R.id.textViewQuestion);
CheckBox checkBox1 = (CheckBox) v.findViewById(R.id.checkBox1);
CheckBox checkBox2 = (CheckBox) v.findViewById(R.id.checkBox2);
...

textViewQuestion.setText(question);
checkBox1.setText(firstAnswer);
checkBox2.setText(secondAnswer);

return v;
}
}

FragmentActivity类代码:

public class KvizPrvaPomoc extends FragmentActivity {

ViewPager pager;
MyPageAdapter pageAdapter;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kviz_prva_pomoc);

pager = (ViewPager)findViewById(R.id.viewpagerquiz);
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), getFragments());
pager.setAdapter(pageAdapter);
}

private List<Fragment> getFragments(){
String question;
String firstAnswer;
String secondAnswer;
...

List<Fragment> fragmentList = new ArrayList<Fragment>();

for (int j=0; j<someList.size(); j++) {
...
fragmentList.add(MyPrvaPomocFragment.newInstance(questionNumber, question, firstAnswer, ...));
}

return fragmentList;
}
}

我已经阅读了许多 SO 问题/答案,但我不知道哪里出了问题。

最佳答案

这是预期的行为; FragmentStatePagerAdapter 会在您向任一方向滚动多个 fragment 远离它们时销毁 fragment 。这样做是为了节省内存。

关于Android ViewPager 获取 View 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20892074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com