我試圖水平滑動頁面,但我得到NullPointerException
。我想刷3頁,我在那些頁面中的textview上使用textview和onclicklistener。當我運行應用程序時,它不斷顯示異常。獲取空指針異常:不明白
這裏是我的logcat
01-08 19:07:21.400: E/AndroidRuntime(22391): FATAL EXCEPTION: main
01-08 19:07:21.400: E/AndroidRuntime(22391): java.lang.NullPointerException
01-08 19:07:21.400: E/AndroidRuntime(22391): at com.programr.dishoom.HotelMap.showHotelMap(HotelMap.java:184)
01-08 19:07:21.400: E/AndroidRuntime(22391): at com.programr.dishoom.HotelMap$MyPagerAdapter.instantiateItem(HotelMap.java:131)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:801)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.support.v4.view.ViewPager.populate(ViewPager.java:930)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.view.View.measure(View.java:12937)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5045)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.view.View.measure(View.java:12937)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5045)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.view.View.measure(View.java:12937)
01-08 19:07:21.400: E/AndroidRuntime(22391): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
這裏是我的全部代碼。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hotel_map);
menuList = (ListView)findViewById(R.id.listView2);
}
MyPagerAdapter adapter = new MyPagerAdapter();
myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(3);
}
private class MyPagerAdapter extends PagerAdapter {
public int getCount(){
return 3;
}
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 = showHotelContact();
break;
case 1:
resId = showHotelAddress();
break;
case 2:
resId = showHotelMap();
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}
}
public int showHotelMap()
{
int resId;
resId = R.layout.clickformap;
TextView clickOnMap = (TextView)findViewById(R.id.clickmap);
clickOnMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent (getApplicationContext(),ShowMap.class);
startActivity(intent);
}
});
return resId;
}
public int showHotelAddress()
{
int resId;
resId = R.layout.hoteladdress;
return resId;
}
public int showHotelContact()
{
int resId;
resId = R.layout.hotelcontact;
return resId;
}
}
我不明白爲什麼我收到此錯誤。
請給我任何提示或參考。
在清單中定義的所有活動。? –
@SahilMahajanMj:是的 –