我试图制作swipe imageview
所以我使用viewpager
。在我的主要布局中,我希望在main_activity.xml
上看到一个按钮。
事情是应用程序崩溃了,我不知道为什么有例外:
(ClassCastException:android.support.v4.view.ViewPager $ LayoutParams 无法转换为android.widget.RelativeLayout $ LayoutParams)。
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/lgray"
android:gravity="clip_vertical|top|bottom|fill"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.plushost.wizgr.MainActivity" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/ImageView3_Left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:src="@drawable/front" />
<android.support.v4.view.ViewPager
android:id="@+id/myviewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
<Button
android:id="@+id/buttonCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="@string/action_something" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends SherlockActivity {
public int currentimageindex=0;
ImageView slidingimage;
private int[] IMAGE_IDS = {
R.drawable.front,
R.drawable.front2
};
ViewPager viewPager;
MyPagerAdapter myPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(".gr");
getSupportActionBar().setLogo(R.drawable.logo);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.myviewpager);
myPagerAdapter = new MyPagerAdapter();
viewPager.setAdapter(myPagerAdapter);
}
private class MyPagerAdapter extends PagerAdapter{
int NumberOfPages = 2;
@Override
public int getCount() {
return NumberOfPages;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(View container, int position) {
ImageView imageView = (ImageView) MainActivity.this.findViewById(R.id.ImageView3_Left);
imageView.setImageResource(IMAGE_IDS[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
}
}
答案 0 :(得分:3)
因为你在已经在RelativeLayout中的视图上使用了findViewById。
ImageView imageView = (ImageView) MainActivity.this.findViewById(R.id.ImageView3_Left);
改为创建一个新的ImageView:
@Override
public Object instantiateItem(View container, int position) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(IMAGE_IDS[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}