Android:Viewpager无法转换为LinearLayout

时间:2016-03-09 10:57:35

标签: android android-viewpager android-volley fresco

您好我正在使用viewpager来显示图片幻灯片。我正在使用凌空从网络加载图像和文本。我收到这样的错误。

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)

我需要在其中显示viewpager。但它经常出现一些错误。请看看我的代码并告诉我我在做错的地方。

我的适配器

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

我的活动文件

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

我的XML文件

<?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>

2 个答案:

答案 0 :(得分:1)

而不是     ((ViewPager)container).addView(itemView);做     container.addView(itemView);

您应该将新项目添加到ViewGroup而不是ViewPager

此外,((ViewPager)container).removeView((LinearLayout) object);应为container.removeView((RelativeLayout) object);

编辑:

尝试更换你的 View itemView = mLayoutInflater.inflate(R.layout.main_activity_pager_layout, container, false);

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

同样在instantiateItem返回itemView而不是container

答案 1 :(得分:0)

更改此行

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

到这个

container.removeView((RelativeLayout) object);