-1

I have A grid View Gallery app with View Pager and Touch View feature enabled. . when a user clicks on a image on grid view it opens in fullscreen.When i click any image in GridView app crashes and this is wht i get in logs
(android.support.v4.view.ViewPager$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams) . Please help.

This is my MainActivity class:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set title for the GridView
        setTitle("GridView");
        // Get the view from grid_view.xml
        setContentView(R.layout.activity_main);

        // Set the images from ImageAdapter.java to GridView
        GridView gridview = (GridView) findViewById(R.id.grid);
        gridview.setAdapter(new ImageAdapter(this));

        // Listening to GridView item click
        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                // Launch ImageViewPager.java on selecting GridView Item
                Intent i = new Intent(getApplicationContext(), ImageViewPager.class);

                // Show a simple toast message for the item position
                Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();

                // Send the click position to ImageViewPager.java using intent
                i.putExtra("id", position);

                // Start ImageViewPager
                startActivity(i);
            }
        });
    }

This is my ImageAdapter class:

public class ImageAdapter extends BaseAdapter  {

    private Context context;

    public String Images[] = {("http://www.fashionlady.in/wp-content/uploads/2016/03/creative-punjabi-mehndi-design-2016.jpg"),
            ("https://lumiere-a.akamaihd.net/v1/images/uk_toystory_chi_woody_n_5b5a006f.png?region=0,0,300,300"),
            ("https://lumiere-a.akamaihd.net/v1/images/open-uri20150422-20810-10n7ovy_9b42e613.jpeg"),
            ("http://www.wetpaint.com/wp-content/uploads/2015/11/toy-story-20th-anniversary.jpg")};


    public ImageAdapter(Context c) {
        context = c;

    }

    @Override
    public int getCount() {
        return Images.length;
    }

    @Override
    public Object getItem(int position) {
        return Images[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageview = new ImageView(context);

        imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageview.setLayoutParams(new GridView.LayoutParams(240, 240));

        Picasso.with(context).load(Images[position]).into(imageview);

        return imageview;
    }
}

This is my FullImageActivity class which should open a full image:

public class FullImageActivity extends AppCompatActivity {

    TouchImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_full_image);


        Intent i = getIntent();

        List<ImageView> images = new ArrayList<>();
        int position = i.getExtras().getInt("id");
        ImageAdapter adapter = new ImageAdapter(this);
        ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);

        img = (TouchImageView) findViewById(R.id.img);
        images.add(img);
        ViewPager viewpager = (ViewPager)findViewById(R.id.pager);
        viewpager.setAdapter(pageradapter);
        viewpager.setCurrentItem(position);
        ViewGroup.LayoutParams params = img.getLayoutParams();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = 100;
        img.requestLayout();

        img.setMaxZoom(4f);
        Picasso.with(getApplicationContext()).load(adapter.Images[position]).into(img);

    }
}

This is my ImagePagerAdapter class:

public class ImagePagerAdapter extends PagerAdapter {

    private List<ImageView> images;

    public ImagePagerAdapter(List<ImageView> images) {
        this.images = images;
    }



    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = images.get(position);
        container.addView(imageView);
        return imageView;
    }

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

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

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

Full error :-

FATAL EXCEPTION: main
                                                                           Process: com.wahid.mehndidesignfinal, PID: 8100
                                                                           java.lang.ClassCastException: android.support.v4.view.ViewPager$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
                                                                               at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:457)
                                                                               at android.view.View.measure(View.java:18788)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                               at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                               at android.view.View.measure(View.java:18788)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                               at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
                                                                               at android.view.View.measure(View.java:18788)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                               at android.view.View.measure(View.java:18788)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                               at android.view.View.measure(View.java:18788)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                               at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
                                                                               at android.view.View.measure(View.java:18788)
                                                                               at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
                                                                               at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
                                                                               at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
                                                                               at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                               at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                               at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                               at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                               at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                               at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
azizbekian
  • 60,783
  • 13
  • 169
  • 249
Frankruss
  • 33
  • 8
  • see: https://stackoverflow.com/questions/2965662/how-do-you-setlayoutparams-for-an-imageview – Florian Hansen May 24 '17 at 12:50
  • You posted `ImageAdapter` twice, instead of `FullImageActivity`. You'll need to provide the complete stack trace, as well. – Mike M. May 24 '17 at 14:09
  • sorry Editted my question – Frankruss May 24 '17 at 14:42
  • In `FullImageActivity`, you're trying to use an `ImageView` that's already attached to the `Activity`'s layout - `R.id.img` - in the `ViewPager`. I'm not sure exactly what you're trying to do, but you can't do that. The `ViewPager` needs its own, separate `View`s. – Mike M. May 24 '17 at 15:20
  • Buddy i removed images.add(img) from fullImageActivity now full image is opening but ViewPager is not working .. – Frankruss May 24 '17 at 15:32
  • Well, now the `Adapter`'s dataset is empty, so I wouldn't expect the `ViewPager` to show anything at all. As I said, I'm not sure what you're trying to do, but perhaps you should practice making a basic `ViewPager` separately first, before you try to incorporate one into this larger project. – Mike M. May 24 '17 at 15:44

1 Answers1

0

use

RelativeLayout.LayoutParams params = img.getLayoutParams();

instead of

ViewGroup.LayoutParams params = img.getLayoutParams();
jigar savaliya
  • 474
  • 1
  • 8
  • 21