我是Android新手。要创建幻灯片,我使用了var div = $('<div/>').html(html);
div.find('<script/>').attr('src', 'other url');
div.find('<link/>').attr('href', 'other url');
。以下是我的代码。
PageAdapter
我的问题出在public class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return 4; //set number of swipe screens here
}
@Override
public Object instantiateItem(final ViewGroup collection, final int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.activity_slide; //set which layout will show on load
break;
case 1:
resId = R.layout.slide_2; //what layout swiping shows
break;
case 2:
resId = R.layout.slide_3; //what layout swiping shows
break;
case 3:
resId = R.layout.slide_4; //what layout swiping shows
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(final ViewGroup arg0, final int arg1, final Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(final View arg0, final Object arg1) {
return arg0 == ((View) arg1);
}
}
我放置了layout.slide_4
,我想点击该按钮打开新活动。所以我在下面写了代码oncreate:
Button
logcat的
Button btnExplore = (Button)findViewById(R.id.imageButton);
btnExplore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vw) {
Intent i = new Intent(IntroSlide.this, StartScreen.class);
startActivity(i);
}
});
我得到了01-18 14:58:58.267 22740-22740/com.totsmart.pedronapp W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x536dd160)
01-18 14:58:58.287 22740-22740/com.totsmart.pedronapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.totsmart.pedronapp, PID: 22740
java.lang.NullPointerException
at com.totsmart.pedronapp.IntroSlide$MyPagerAdapter.instantiateItem(IntroSlide.java:59)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:251)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:771)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:543)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:757)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5045)
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:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
。我哪里错了?
答案 0 :(得分:3)
根据我们在注释中的讨论,您的布局将在运行时加载。因此,如果未加载任何布局,并且您尝试使用该布局的任何视图,您将获得NullPointerException
。
我建议在设置onClickListener
之前添加一个空检查
Button btnExplore = (Button)findViewById(R.id.imageButton);
if(btnExplore != null)
{
btnExplore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vw) {
Intent i = new Intent(IntroSlide.this, StartScreen.class);
startActivity(i);
}
});
}
注意:您应该将所有这些代码放在一个单独的方法中,并在每次加载新布局时调用该方法。
更新:更好的解决方案是根据各个布局划分侦听器代码,并为不同布局的视图侦听器保留不同的方法。在此之后,从switch语句中调用适当的方法。此外,您还必须在个别情况下移动以下代码。
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;