0

Hi I am using viewpager to show a image slide. And I am loading the images and text from the network using volley. I am getting the error like this.

java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to android.widget.LinearLayout
                                                                     at com.truetech.lola.adapter.HomeCentralPagerAdapter.destroyItem(HomeCentralPagerAdapter.java:70)
                                                                     at android.support.v4.view.ViewPager.populate(ViewPager.java:1111)
                                                                     at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)

And I need to show the viewpager in it. But its often showing some error. Kindly look at my code and tell me where I am doing the mistake.

And My Adapter is

public class HomeCentralPagerAdapter extends PagerAdapter {

private Context mContext;
private LayoutInflater mLayoutInflater;
private List<HomeCentralPagerContent> mHomeCentralPagerContents;

public HomeCentralPagerAdapter(Context mContext) {
    this.mContext = mContext;
    mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public HomeCentralPagerAdapter(Context mContext, List<HomeCentralPagerContent> mHomeCentralPagerContents) {
    this.mContext = mContext;
    this.mHomeCentralPagerContents = mHomeCentralPagerContents;
}

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

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = mLayoutInflater.inflate(R.layout.main_activity_pager_layout, container, false);

    //ImageView homeCentralPagerIMV = (ImageView) itemView.findViewById(R.id.homeCentralImage);
    SimpleDraweeView homeCentralDraweeView = (SimpleDraweeView) itemView.findViewById(R.id.homeCentralImage);
    TextView homeCentralPointsTV = (TextView) itemView.findViewById(R.id.homeCentralPoints);
    TextView homeCentralTitleTV = (TextView) itemView.findViewById(R.id.homeCentralPagerTitle);

    HomeCentralPagerContent m = mHomeCentralPagerContents.get(position);
    Uri imageUri = Uri.parse(m.getHomeCentralImage());
    homeCentralDraweeView.setImageURI(imageUri);
    homeCentralPointsTV.setText(m.getHomeCentralPoints());

    ((ViewPager)container).addView(itemView);

    return container;
}

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

And My Activity file is

public class MainActivity extends Activity {

private static final String TAG = "MainActivity";

private PrefManager pref;
private HomeCentralPagerAdapter mHomeCentralPagerAdapter;
private List<HomeCentralPagerContent> pagerContentList = new    ArrayList<HomeCentralPagerContent>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pref = new PrefManager(getApplicationContext());
    ViewPager mViewPager = (ViewPager) findViewById(R.id.homeCentralViewPager);
    mHomeCentralPagerAdapter = new HomeCentralPagerAdapter(this, pagerContentList);
    getHomeCentralContents();
    mViewPager.setAdapter(mHomeCentralPagerAdapter);
}

private void getHomeCentralContents() {
    String URL = ConstantValues.GetHomeCentralContent + pref.getUserId();
    Log.d(TAG, "Home Central URL: " + URL);

    new NetWorkRequest(MainActivity.this).volleyGetJsonObjectData(URL, new VolleyResponseListerner() {
        @Override
        public void onResponse(JSONObject response) throws JSONException {
            VolleyLog.d(TAG, "Home Central OnResponse: " + response);

            String statusStr = response.getString("Status");
            Log.d(TAG, "Status: " + statusStr);

            if (statusStr.equalsIgnoreCase("Success")) {

                Iterator iterator = response.keys();
                while (iterator.hasNext()) {
                    String key = (String) iterator.next();
                    Log.d(TAG, "Key Value: " + key);
                    if (response.get(key) instanceof JSONArray) {
                        Log.d(TAG, "JSON Array Key: " + key);

                        try {
                            if (!key.equalsIgnoreCase("Point Details")) {
                                JSONArray loopedArray = response.getJSONArray(key);
                                JSONObject jsonObject;

                                for (int i = 0; i < loopedArray.length(); i++) {
                                    jsonObject = loopedArray.getJSONObject(i);

                                    HomeCentralPagerContent homeCentralPagerContent = new HomeCentralPagerContent();
                                    homeCentralPagerContent.setHomeCentralPoints(jsonObject.getString(key + "_Points"));
                                    homeCentralPagerContent.setHomeCentralImage(jsonObject.getString("ImagePath"));

                                    pagerContentList.add(homeCentralPagerContent);
                                }
                            } else {
                                Log.d(TAG, "Exception Due to Points Details Array");
                            }
                        } catch (JSONException e) {
                            Log.e(TAG, "JSON Array Key Exception: " + e);
                        }
                    }
                }

                mHomeCentralPagerAdapter.notifyDataSetChanged();
                Log.d(TAG, "Total Content List: " + pagerContentList);
            }
        }

        @Override
        public void onError(String message, String title) {
            VolleyLog.e(TAG, "Home Central OnErrorResponse: " + message);
        }
    });
}
}

And My XML File is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainPagerParentLay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/homeCentralImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    fresco:placeholderImage="@drawable/logo_login" />

<RelativeLayout
    android:id="@+id/homeCentralPointsAndTitleLay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="33dp"
    android:layout_marginRight="33dp"
    android:layout_marginTop="397dp">

    <TextView
        android:id="@+id/homeCentralPoints"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/round_counter_bg"
        android:gravity="center"
        android:text=""
        android:textColor="#fff"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/homeCentralPagerTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/homeCentralPoints"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:textColor="@color/dark_grey"
        android:textSize="14sp" />
</RelativeLayout>

</RelativeLayout>
Encipherer
  • 411
  • 7
  • 23

2 Answers2

1

Instead of ((ViewPager)container).addView(itemView); do container.addView(itemView);

You should add the new item to the ViewGroup not the ViewPager

Also, ((ViewPager)container).removeView((LinearLayout) object); should be container.removeView((RelativeLayout) object);

Edit:

Try replacing your View itemView = mLayoutInflater.inflate(R.layout.main_activity_pager_layout, container, false);

with

View itemView = mLayoutInflater.inflate(R.layout.main_activity_pager_layout, null);

Also in your instantiateItem return itemView not container

vkislicins
  • 3,331
  • 3
  • 32
  • 62
  • tried bro, still same error java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to android.widget.RelativeLayout at com.truetech.lola.adapter.HomeCentralPagerAdapter.destroyItem(HomeCentralPagerAdapter.java:69) – Encipherer Mar 09 '16 at 11:08
  • @IndependentDev Are you sure you've changed both lines? You can only receive this error if you're still attempting to cast your container to a `ViewPager` – vkislicins Mar 09 '16 at 11:10
  • S I did, inside instantiate view I added this container.addView(itemView); return container; and Inside the public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((RelativeLayout) object); } – Encipherer Mar 09 '16 at 11:19
  • If you did that, you should be getting the cast error. Try cleaning your project, uninstalling the app from the device, reinstalling the app fresh after the project got cleaned. If you still have errors, paste the error – vkislicins Mar 09 '16 at 11:22
  • can u have a look at my whole activity class. Tell me whether the way I am using to get the data is correct or not – Encipherer Mar 09 '16 at 11:31
  • same error in destroy view java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to android.widget.RelativeLayout at com.truetech.lola.adapter.HomeCentralPagerAdapter.destroyItem(HomeCentralPagerAdapter.java:69) – Encipherer Mar 09 '16 at 11:33
  • `View itemView = mLayoutInflater.inflate(R.layout.main_activity_pager_layout, container, false);` Try replacing with `View itemView = mLayoutInflater.inflate(R.layout.main_activity_pager_layout, null);` Also return `itemView` not `container` – vkislicins Mar 09 '16 at 11:37
  • Thanks a lot bro.... Its working.... I just changed the convert view to itemview. and its working. Even if I change the inflater line like u mentioned also its working. Thanks again :) :) :) – Encipherer Mar 09 '16 at 11:45
0

Change this line

((ViewPager)container).removeView((LinearLayout) object);

to this

container.removeView((RelativeLayout) object);
Andy Joyce
  • 2,802
  • 3
  • 15
  • 24
  • still same kind of error java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to android.widget.RelativeLayout at com.truetech.lola.adapter.HomeCentralPagerAdapter.destroyItem(HomeCentralPagerAdapter.java:69) – Encipherer Mar 09 '16 at 11:07
  • @IndependentDev You also add to `ViewPager` instead of the `ViewGroup`. You should correct in both places. Please see my answer – vkislicins Mar 09 '16 at 11:08
  • inside instantiate view I added this container.addView(itemView); return container; and Inside the public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((RelativeLayout) object); } @AndyJoyce this is what I did – Encipherer Mar 09 '16 at 11:20