我正在按照教程了解ViewPager
的工作原理。当我运行应用程序时没有问题,但是当我滑动移动到Pager
应用程序崩溃的第二页时,Logcat
会生成以下错误。如果我错过了什么,请查看代码并告诉我。
ViewPager_Activity:
public class ViewPager00 extends FragmentActivity {
private MyAdapter mAdapter;
private ViewPager mPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
switch (position) {
case 0:
return new DetailFragment();
case 1:
return new ImageFragment();
default:
return null;
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
}
Details_Fragment:
public class DetailFragment extends Fragment {
private static final String TAG = "DetailFragment";
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.e(TAG, "@onCreate(): Hello");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.details,container, false);
TextView textview = (TextView) view.findViewById(R.id.detailsText);
textview.setText("Testing");
return view;
}
}
Image_Fragment:
public class ImageFragment extends Fragment {
private static final String TAG = "ImageFragment";
//private final int imageResourceId;
//public ImageFragment() {}
/*public ImageFragment(int imageResourceId) {
this.imageResourceId = imageResourceId;
}*/
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.e(TAG, "@onCreate(): Hello");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.image,container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
//imageView.getResources().getDrawable(R.drawable.ic_launcher);
return view;
}
}
logcat的:
06-23 12:45:32.995: E/AndroidRuntime(856): FATAL EXCEPTION: main
06-23 12:45:32.995: E/AndroidRuntime(856): Process: com.example.viewpager00, PID: 856
06-23 12:45:32.995: E/AndroidRuntime(856): java.lang.NullPointerException
06-23 12:45:32.995: E/AndroidRuntime(856): at
android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
06-23 12:45:32.995: E/AndroidRuntime(856): at
android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
06-23 12:45:32.995: E/AndroidRuntime(856): at
android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
06-23 12:45:32.995: E/AndroidRuntime(856): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
06-23 12:45:32.995: E/AndroidRuntime(856): at android.support.v4.view.ViewPager.populate(ViewPager.java:1048)
06-23 12:45:32.995: E/AndroidRuntime(856): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
06-23 12:45:32.995: E/AndroidRuntime(856): at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
06-23 12:45:32.995: E/AndroidRuntime(856): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
答案 0 :(得分:4)
您的getCount()
返回3,但getItem()
仅为索引0和1返回非空值。将计数更改为2或使getItem()
返回三项。