我正在尝试水平滑动页面但我得到了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;
}
}
我不明白为什么我会收到这个错误 请给我任何提示或参考。
答案 0 :(得分:2)
您呼叫findViewById()
的上下文位于“活动”中。这会检查您通过活动的setContent()
方法在onCreate()
中设置的主要布局。如果TextView R.id.clickmap
不是您传递给Activity的视图的一部分,则无法找到它。
Activity类本身不知道在PagerAdapter中创建/销毁的视图,因此无法找到它们。为了使findViewById()
起作用,您需要在对包含该类的父视图的引用上调用它。它可以是View层次结构中的任何父级,但它必须是父级或视图本身。
编辑:
我不知道您是如何调用showHotelMap()
的,但是您需要使用包含您正在查找的TextView的父视图:
View mainView = getMainView()
TextView clickOnMap = (TextView) mainView.findViewById(R.id.clickmap);
clickOnMap.setOnClickListener(......);
其中,getMainView()
是一种检索所需视图引用的方法。您如何做到这取决于您的实施。
编辑2:
private class MyPagerAdapter extends PagerAdapter {
...
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View returnView = null;
switch (position) {
case 0:
returnView = showHotelContact(inflater);
break;
...
}
return returnView;
}
...
public View showHotelMap(Inflater inflater)
{
View returnView = inflater.inflate(R.layout.clickformap, null);
TextView clickOnMap = (TextView)returnView.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 returnView;
}
}
在此实施中,您要在方法中对View
进行夸大,设置它,然后将其返回到PagerAdapter
。您无需将其添加到ViewPager,因为ViewPager本身将调用instantiateItem()
并将其添加到自身。
答案 1 :(得分:1)
您在
中有 NullPointerExceptionTextView clickOnMap = (TextView)findViewById(R.id.clickmap);
clickOnMap.setOnClickListener(new View.OnClickListener()...
可能 R.id.clickmap 不是 TextView 的正确ID, findViewById 返回null clickOnMap ,然后尝试 setOnClickListener 为空对象。
答案 2 :(得分:1)
如果您想使用findViewByID()
,则需要通过其父视图引用它。
你应该使用这样的东西,
TextView clickOnMap = (TextView)ActivityName.this.findViewById(R.id.clickmap);