ViewPager strife
I'm trying to create a custom ViewPager in my app. I've looked at the following post and this appears to be what I'm trying to do. Custom ViewPager
I'm getting a ClassCastException when it runs. I'm trying to inflate a linear layout and I'm guessing this is the problem as it's not actually a view, it's a viewgroup. please can someone tell me where I'm going wrong?
Thanks,
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView1" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView2" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView2"></TextView>
</LinearLayout>
--
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater)
cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.animallayout, null);
TextView tv1 = (TextView) layout.findViewById(R.id.textView1);
TextView tv2 = (TextView) layout.findViewById(R.id.textView2);
tv1.setText("test 1");
tv2.setText("test 2");
((ViewPager) collection).addView(layout, 0);
return layout;
}
ERROR:
09-01 20:12:13.004: ERROR/AndroidRuntime(437): FATAL EXCEPTION: main
09-01 20:12:13.004: ERROR/AndroidRuntime(437): java.lang.ClassCastException: android.widget.RelativeLayout
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at com.msi.awesomepager.AwesomePagerActivity$AwesomePagerAdapter.instantiateItem(AwesomePagerActivity.java:61)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:321)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at android.support.v4.view.ViewPager.populate(ViewPager.java:441)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at android.support.v4.view.ViewPager.onAttachedToWindow(ViewPager.java:563)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at android.view.View.dispatchAttachedToWindow(View.java:6156)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1122)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-01 20:12:13.00开发者_开发问答4: ERROR/AndroidRuntime(437): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at android.view.ViewRoot.performTraversals(ViewRoot.java:765)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at android.os.Looper.loop(Looper.java:123)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at android.app.ActivityThread.main(ActivityThread.java:3647)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at java.lang.reflect.Method.invoke(Method.java:507)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-01 20:12:13.004: ERROR/AndroidRuntime(437): at dalvik.system.NativeStart.main(Native Method)
The Problem that I was having here was to do with the casting in two of the overridden methods
see this post for more details Displaying horizontal scrolling view of a linear layout.
精彩评论