如何解决viewpager android中的null对象引用问题

时间:2014-12-23 05:04:56

标签: android xml android-layout android-activity android-viewpager

我正在使用查看寻呼机来刷图像。我可以在Log中获取url,但是当我尝试在ImageView中设置时,我收到错误。

        E/AndroidRuntime(1895): FATAL EXCEPTION: main
   E/AndroidRuntime(1895): Process: com.project.center, PID: 1895
    E/AndroidRuntime(1895): java.lang.NullPointerException: Attempt to read from field    com.project.center.views.TouchImageView com.project.center.FullScreenActivity$ViewHolder.mImageView' on a null object reference
   E/AndroidRuntime(1895):  at   com.project.center.FullScreenActivity$ViewPagerAdapter.instantiateItem(FullScreenActivity.java:82)
   : E/AndroidRuntime(1895):    at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
    E/AndroidRuntime(1895):     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
   E/AndroidRuntime(1895):  at android.support.v4.view.ViewPager.populate(ViewPager.java:987)
   E/AndroidRuntime(1895):  at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
   E/AndroidRuntime(1895):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
   E/AndroidRuntime(1895):  at android.view.View.measure(View.java:17430)
    E/AndroidRuntime(1895):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
 : E/AndroidRuntime(1895):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
  E/AndroidRuntime(1895):   at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
  E/AndroidRuntime(1895):   at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
   E/AndroidRuntime(1895):  at android.view.View.measure(View.java:17430)
   E/AndroidRuntime(1895):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
   E/AndroidRuntime(1895):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
   E/AndroidRuntime(1895):  at android.view.View.measure(View.java:17430)
   E/AndroidRuntime(1895):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
    E/AndroidRuntime(1895):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:447)
    E/AndroidRuntime(1895):     at android.view.View.measure(View.java:17430)
    E/AndroidRuntime(1895):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
    E/AndroidRuntime(1895):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)

我在代码中检查了很多次,但我无法解决错误。

public class FullScreenActivity extends Activity {
private ViewPager viewPager;
ViewPagerAdapter viewpageradapter;
ArrayList<String> imageUrl;
private Intent mIntent;
private int imagePosition;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_viewpager);
    mIntent = getIntent();
    imageUrl = mIntent.getStringArrayListExtra("url");
    imagePosition = mIntent.getIntExtra("imageurl", 0);
    viewPager = (ViewPager) findViewById(R.id.myfivepanelpager);
    viewpageradapter = new ViewPagerAdapter(this, imageUrl);
    viewPager.setAdapter(viewpageradapter);
}

class ViewPagerAdapter extends PagerAdapter {
    Activity activity;
    ArrayList<String> imgArray;
    public ImageLoader imageLoader;
    ViewHolder viewHolder;

    public ViewPagerAdapter(Activity act, ArrayList<String> imgArra) {
        imgArray = imgArra;
        activity = act;
        imageLoader = new ImageLoader(act);
        aquery = new AQuery(act);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return imgArray.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0 == ((View) arg1);
    }

    public Object instantiateItem(View convertView, int position) {

        LayoutInflater layoutinflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
             viewHolder = new ViewHolder();
            convertView = layoutinflater.inflate(
                    R.layout.image_viewpager_row, null);
            viewHolder.mImageView = (TouchImageView) convertView
                    .findViewById(R.id.imageViews);
            convertView.setTag(viewHolder);
        } else
            viewHolder = (ViewHolder) convertView.getTag();

        String imgUrl = imgArray.get(position);
         Log.e("imgUrl",imgUrl); // i can get image url here.
        imageLoader.DisplayImage(imgUrl, viewHolder.mImageView);
        return convertView;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public Parcelable saveState() {
        return null;
    }


}
static class ViewHolder {
    public TouchImageView mImageView;
}

}

这是viewpager xml:

   

 <android.support.v4.view.ViewPager
    android:id="@+id/myfivepanelpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

和image_viewpager.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.project.center.TouchImageView
        android:id="@+id/imageViews"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:2)

尝试更改您的instantiateItem代码

  public Object instantiateItem(View container2, int position) {

    LayoutInflater layoutinflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View ve = layoutinflater.inflate(R.layout.image_viewpager_row, null);

    TouchImageView  mImageView = (TouchImageView) ve.findViewById(R.id.imageViews);

    String imgUrl = imgArray.get(position);
     Log.e("imgUrl",imgUrl); // i can get image url here.
    imageLoader.DisplayImage(imgUrl, mImageView);

    ((ViewPager) container2).addView(ve, 0);

    return ve;
}

此处无需使用ViewHolder

答案 1 :(得分:1)

instantiateItem的第一个参数是ViewGroup容器,而不是convertView。您无法直接从PagerAdapter使用convertView / viewHolder回收模式。您需要重写该代码。

我建议您使用此ViewPagerAdapter来帮助您完成此操作。

答案 2 :(得分:1)

无需使用ViewHolder类和setTag以及getTag方法。修改instantiateItem方法以将视图添加到当前所选屏幕:

public Object instantiateItem(View container2, int position) {

    LayoutInflater layoutinflater = (LayoutInflater) 
                 activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View ve=layoutinflater.inflate(R.layout.image_viewpager_row, null);
    TouchImageView mImageView = (TouchImageView)ve.findViewById(R.id.imageViews);
    String imgUrl = imgArray.get(position);
    Log.e("imgUrl",imgUrl); // i can get image url here.
    imageLoader.DisplayImage(imgUrl, mImageView);

    ((ViewPager) container2).addView(ve, 0);

    return ve;
}