0

我正在尝试水平滑动页面,但我得到了NullPointerException。我正在尝试滑动 3 页,我在其中一个页面中的那个 textview 上使用 textview 和 onclicklistener。当我运行应用程序时,它不断显示异常。

这是我的日志猫

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;
}

}

我不明白为什么我会收到这个错误。
请给我任何提示或参考。

4

3 回答 3

2

您调用的上下文在findViewById()Activity 中。这将检查您setContent()通过onCreate()Activity 的方法设置的主布局。如果 TextViewR.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()并将其添加到自身。

于 2013-01-08T13:52:07.213 回答
1

你有一个NullPointerException在:

TextView clickOnMap = (TextView)findViewById(R.id.clickmap);
clickOnMap.setOnClickListener(new View.OnClickListener()...

可能R.id.clickmap不是您的TextView的正确 ID,并且findViewById将 null 返回到clickOnMap,然后您尝试将setOnClickListener设置为 null 对象。

于 2013-01-08T13:54:08.427 回答
1

如果你想使用findViewByID(),你需要通过它的父视图来引用它。

你应该使用这样的东西,

TextView clickOnMap = (TextView)ActivityName.this.findViewById(R.id.clickmap);
于 2013-01-08T14:01:15.397 回答