androidandroid-viewpagertouchimageview

TouchImageView is not working for ViewPager


I want to implement pinch zoom effect on an imageView inside a ViewPager. I get the code of how to zoom an image. but when i implement that code inside a ViewPage it didnt work. I have used TouchImageView Class its not working. I have google it for my error,its not satifised me. I couldn't see my image its shows error..

LogCat:

06-06 16:50:44.570: E/AndroidRuntime(30756): FATAL EXCEPTION: main
06-06 16:50:44.570: E/AndroidRuntime(30756): java.lang.NullPointerException
06-06 16:50:44.570: E/AndroidRuntime(30756):  at com.androidbegin.viewpagergridview.ImagePagerAdapter.instantiateItem(ImagePagerAdapter.java:40)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.View.measure(View.java:15504)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.View.measure(View.java:15504)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.View.measure(View.java:15504)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.View.measure(View.java:15504)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2385)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.View.measure(View.java:15504)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1979)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1222)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4526)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.Choreographer.doFrame(Choreographer.java:525)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.os.Handler.handleCallback(Handler.java:615)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.os.Looper.loop(Looper.java:137)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.app.ActivityThread.main(ActivityThread.java:4921)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at java.lang.reflect.Method.invokeNative(Native Method)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at java.lang.reflect.Method.invoke(Method.java:511)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at dalvik.system.NativeStart.main(Native Method)

ImagePagerAdapater.class

public class ImagePagerAdapter extends PagerAdapter {

        private Activity _activity;
         private LayoutInflater inflater;
    private List<ImageView> images;


    public ImagePagerAdapter(Activity activity,
              List<ImageView> images) {
          this._activity = activity;
          this.images = images;
         }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        TouchImageView imgDisplay;
         inflater = (LayoutInflater) _activity
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View viewLayout = inflater.inflate(R.layout.view_pager, container,
                 false); //Line number 40

         imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);

         ((ViewPager) container).addView(viewLayout);

         return viewLayout;
    }



        @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
       // container.removeView(images.get(position));
        ((ViewPager) container).removeView((RelativeLayout) object);
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }
  }

ImageAdapter.class

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return mThumbIds[position];
    }

    public long getItemId(int position) {
        return 0;
    }

    // Create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // If it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
           // imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // References to our images in res > drawable
    public Integer[] mThumbIds = {
            R.drawable.small, R.drawable.small1,
            R.drawable.small2, R.drawable.small3,
            R.drawable.small4, R.drawable.small5,
            R.drawable.small6, R.drawable.small7,
            R.drawable.small8, R.drawable.small9};
}

xml

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

    <com.androidbegin.viewpagergridview.TouchImageView
        android:id="@+id/imgDisplay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitCenter" />


</RelativeLayout>

Please guide me. What im doing mistake? Thanks a lot in advance..


Solution

  • 1.Why you are getting NullPointerException(NPE)? Inflater is null. Please check the condition

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
    
        TouchImageView imgDisplay;
         //Here is the problem i hope
         //  inflater = (LayoutInflater) _activity
         //        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         //try using getContext() instead _activity
           inflater  = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
         //check whether inflater is null or not
          if(inflater!=null)
              {
                 View viewLayout = inflater.inflate(R.layout.view_pager, container,
                 false); //Line number 40
    
                 imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
    
                ((ViewPager) container).addView(viewLayout);
    
              return viewLayout;
    
               }
             }
    

    2.How to set image position to dispaly? as you discussed in comments. Have a look at here

    Pager has some override methods with setOnPageChangeListener. You can get the current position of the image after the page has been scrolled

       mViewPager.setOnPageChangeListener(new SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, 
                                       int positionOffsetPixels) {
    
            }
    
            @Override
            public void onPageSelected(int position) {
    
            }
    
            @Override
            public void onPageScrollStateChanged(int state) {
                         //check for the state
                        if (state == SCROLL_STATE_SETTLING) {
    
                               // Here code for the image zooming
                             }
    
            }
        });
    

    Please use debugger and see what these override methods are doing. Then you will be getting some idea on viewpager. Hope this helps for you