我的自定义pageadapter
正在NullPointerException
获得instantiateItem
。但是,如果我将此适配器用作内部类,则不会出现错误。
我的自定义pageadapter类
class MyPagesAdapter extends PagerAdapter {
String mPageData[]; //Stores the text to swipe.
LayoutInflater inflater; //Used to create individual pages
ViewPager vp;
public MyPagesAdapter(String pageData[])
{
mPageData = pageData;
}
@Override
public int getCount() {
return mPageData.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View page = inflater.inflate(R.layout.page, null);
((TextView)page.findViewById(R.id.textMessage)).setText(mPageData[position]);
((ViewPager) container).addView(page, 0);
return page;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==(View)arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
object=null;
}
}
这是mainActivity类
public class MainActivity extends Activity {
String pageData[]; //Stores the text to swipe.
LayoutInflater inflater; //Used to create individual pages
ViewPager vp; //Reference
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pageData=getResources().getStringArray(R.array.desserts);
//get an inflater to be used to create single pages
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Reference ViewPager defined in activity
vp=(ViewPager)findViewById(R.id.viewPager);
//set the adapter that will create the individual pages
vp.setAdapter(new MyPagesAdapter(pageData));
}}
这是错误:
07-03 10:02:07.269: D/dalvikvm(1391): Not late-enabling CheckJNI (already on)
07-03 10:02:07.289: D/AndroidRuntime(1391): Shutting down VM
07-03 10:02:07.289: W/dalvikvm(1391): threadid=1: thread exiting with uncaught exception (group=0xb5004180)
07-03 10:02:07.339: E/AndroidRuntime(1391): FATAL EXCEPTION: main
07-03 10:02:07.339: E/AndroidRuntime(1391): java.lang.NullPointerException
07-03 10:02:07.339: E/AndroidRuntime(1391): at com.example.viewpager2.MyPagesAdapter.instantiateItem(MyPagesAdapter.java:30)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:869)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.support.v4.view.ViewPager.populate(ViewPager.java:1019)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.view.View.measure(View.java:12723)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.view.View.measure(View.java:12723)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.view.View.measure(View.java:12723)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.view.View.measure(View.java:12723)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-03 10:02:07.339: E/AndroidRuntime(1391): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.view.View.measure(View.java:12723)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.os.Looper.loop(Looper.java:137)
07-03 10:02:07.339: E/AndroidRuntime(1391): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-03 10:02:07.339: E/AndroidRuntime(1391): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 10:02:07.339: E/AndroidRuntime(1391): at java.lang.reflect.Method.invoke(Method.java:511)
07-03 10:02:07.339: E/AndroidRuntime(1391): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-03 10:02:07.339: E/AndroidRuntime(1391): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-03 10:02:07.339: E/AndroidRuntime(1391): at dalvik.system.NativeStart.main(Native Method)
07-03 10:02:07.780: I/dalvikvm(1391): threadid=3: reacting to signal 3
07-03 10:02:07.780: I/dalvikvm(1391): Wrote stack traces to '/data/anr/traces.txt'
07-03 10:02:07.958: I/dalvikvm(1391): threadid=3: reacting to signal 3
07-03 10:02:07.958: I/dalvikvm(1391): Wrote stack traces to '/data/anr/traces.txt'
更新
一个解决方案可能是将inflater传递给来自活动的pageadapter类
vp.setAdapter(new MyPagesAdapter(pageData,inflater));
pageadapter类中的
public MyPagesAdapter(String pageData[], LayoutInflater minflater)
{
mPageData = pageData;
inflater = minflater;
}
或简单地在pageadapter类
中建立LayoutinflaterLayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);