我正在使用 PagerAdpater 在Android中显示视图寻呼机。当我滑动页面但是当我通过寻呼机选择特定页面时,它工作得很好。我的应用程序崩溃之后的setCurrentItem()方法。在调试代码之后我们纠正了寻呼机添加pet并没有返回inflate视图,所以我们不从XML文件中获取它。
这是我的寻呼机适配器
public class CustomPagerAdapter extends PagerAdapter {
public Object instantiateItem(ViewGroup collection, int position) {
int resId = 0;
switch (position) {
case 0: {
resId = R.layout.scrollable_list;
Log.i("name", "page3" + resId);
break;
}
case 1: {
resId = R.layout.page0;
break;
}
case 2: {
resId = R.layout.page3;
Log.i("name", "page3" + resId);
break;
}
case 3: {
Log.i("AFlow", "excuting");
resId = R.layout.page4;
Log.i("name", "page4" + resId);
break;
}
}
View view = (View) inflater.inflate(resId, null);
collection.addView(view, 0);
return view;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public int getCount() {
return 4;
}
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
@Override
public void notifyDataSetChanged() {
// TODO Auto-generated method stub
super.notifyDataSetChanged();
}
}
这是我的Log Cat
06-27 07:28:46.475 26166-26166/com.demol.viewpager E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.demol.viewpager, PID: 26166
java.lang.NullPointerException
at com.demol.viewpager.IntroductionFragment.addlist(IntroductionFragment.java:2429)
at com.demol.viewpager.IntroductionFragment.onCreateView(IntroductionFragment.java:163)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
调试后我发现这个问题,当我刷页面列表视图返回id但直接选择页面列表视图返回null id我认为这是主要问题
任何帮助,建议,教程都将受到高度赞赏。谢谢....