我已经为此工作了一个星期,但我无法弄清楚。我有一个有 4 个视图的寻呼机适配器。它让我在它们之间滑动。但是当我调用 onClickListener 时,应用程序在启动时崩溃,因为 Listener 为空。我是一名新程序员,我不明白如何解决这个问题。请帮忙!我已经隔离了问题的根源,在侦听器查找按钮 ID 后视图没有被创建或被破坏,这就是它为空和崩溃的原因。
方法 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);
}
我的另一个问题是如何使用该按钮加载新视图或活动?它不允许我实现活动,这是我学会如何调用捆绑包的唯一方法。提前致谢!