我已经在这方面工作了一个星期,我无法理解。我有一个有4个视图的寻呼机适配器。它让我可以在它们之间滑动。但是当我调用onClickListener时,应用程序在启动时崩溃,因为Listener为null。我是一名新程序员,我不明白如何解决这个问题。请帮忙!我已经隔离了问题的根源,在Listener查找按钮ID之后,视图没有被创建或被销毁,这就是为什么它是null并且崩溃的原因。
方法destroyItem是我认为导致它的原因,因为当我删除它时,应用程序不会崩溃,直到我开始刷视图。
public class SplashPager extends PagerAdapter {
Button b;
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.splash2;
b = (Button) collection.findViewById(R.id.splashB);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//setContentView(r.layout.someLayout);
}
});
break;
case 1:
resId = R.layout.splash3;
break;
case 2:
resId = R.layout.splash4;
break;
case 3:
resId = R.layout.splash5;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
// TODO Auto-generated method stub
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == ((View) arg1);
}
}
日志:
01-17 21:03:35.179: W/dalvikvm(16226): threadid=1: thread exiting with uncaught exception (group=0x401a2560)
01-17 21:03:35.179: E/AndroidRuntime(16226): FATAL EXCEPTION: main
01-17 21:03:35.179: E/AndroidRuntime(16226): java.lang.NullPointerException
01-17 21:03:35.179: E/AndroidRuntime(16226): at com.example.survtest2.SplashPager.instantiateItem(SplashPager.java:33)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:801)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.support.v4.view.ViewPager.populate(ViewPager.java:930)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.view.View.measure(View.java:8424)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.view.View.measure(View.java:8424)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.view.View.measure(View.java:8424)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.view.View.measure(View.java:8424)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.view.ViewRoot.performTraversals(ViewRoot.java:844)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.view.ViewRoot.handleMessage(ViewRoot.java:1864)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.os.Looper.loop(Looper.java:130)
01-17 21:03:35.179: E/AndroidRuntime(16226): at android.app.ActivityThread.main(ActivityThread.java:3733)
01-17 21:03:35.179: E/AndroidRuntime(16226): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 21:03:35.179: E/AndroidRuntime(16226): at java.lang.reflect.Method.invoke(Method.java:507)
01-17 21:03:35.179: E/AndroidRuntime(16226): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
01-17 21:03:35.179: E/AndroidRuntime(16226): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:650)
01-17 21:03:35.179: E/AndroidRuntime(16226): at dalvik.system.NativeStart.main(Native Method)
01-17 21:03:35.199: E/AndroidRuntime(16226): [Blue Error Handler] Make Debugging Report file for main
01-17 21:03:35.199: E/AndroidRuntime(16226): java.lang.NullPointerException
01-17 21:03:35.199: E/AndroidRuntime(16226): at com.example.survtest2.SplashPager.instantiateItem(SplashPager.java:33)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:801)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.support.v4.view.ViewPager.populate(ViewPager.java:930)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.view.View.measure(View.java:8424)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.view.View.measure(View.java:8424)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.view.View.measure(View.java:8424)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.view.View.measure(View.java:8424)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.view.ViewRoot.performTraversals(ViewRoot.java:844)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.view.ViewRoot.handleMessage(ViewRoot.java:1864)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.os.Looper.loop(Looper.java:130)
01-17 21:03:35.199: E/AndroidRuntime(16226): at android.app.ActivityThread.main(ActivityThread.java:3733)
01-17 21:03:35.199: E/AndroidRuntime(16226): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 21:03:35.199: E/AndroidRuntime(16226): at java.lang.reflect.Method.invoke(Method.java:507)
01-17 21:03:35.199: E/AndroidRuntime(16226): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
01-17 21:03:35.199: E/AndroidRuntime(16226): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:650)
01-17 21:03:35.199: E/AndroidRuntime(16226): at dalvik.system.NativeStart.main(Native Method)
我很确定这是问题的原因,但我不知道这种方法是如何工作的或如何解决它:
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
// TODO Auto-generated method stub
((ViewPager) arg0).removeView((View) arg2);
}
我的另一个问题是如何使用该按钮加载新视图或活动?它不允许我实现活动,这是我学习如何调用bundle的唯一方法。提前谢谢!
答案 0 :(得分:1)
尝试将destroyItem更改为:
@Override
public void destroyItem(View arg0, int arg1, Object arg2){
((ViewPager) arg0).removeViewAt(arg1);
}
和isViewFromObject:
@Override
public boolean isViewFromObject(View view, Object object){
return view.equals(object);
}
如果这些更改不起作用,那么我建议你检查一下你可以在集合View上“findViewById”后会发生什么。看起来你看的按钮不在那个对象内。您必须在正在膨胀的视图中搜索它。
尝试使用此代码作为实例化代码:
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
View view = null;
switch (position) {
case 0:
view = inflater.inflate(R.layout.splash2, null);
b = (Button) view.findViewById(R.id.splashB);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//setContentView(r.layout.someLayout);
}
});
break;
case 1:
view = inflater.inflate(R.layout.splash3, null);
break;
case 2:
view = inflater.inflate(R.layout.splash4, null);
break;
case 3:
view = inflater.inflate(R.layout.splash5, null);
break;
}
((ViewPager) collection).addView(view, 0);
return view;
}