Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot cast to custom ViewPager class

I'm using this source from Chrisbanes Github: https://github.com/chrisbanes/PhotoView and tried making ViewPager with LinearLayout but it's getting error when casting ViewPager to HackyViewPager Here's my xml (bacaan.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="7" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="horizontal" >
        ...
    </LinearLayout>
</LinearLayout>

on the ViewPagerActivity.java , I changed this:

mViewPager = new HackyViewPager(this);
setContentView(mViewPager);
mViewPager.setAdapter(new SamplePagerAdapter());

to this:

setContentView(R.layout.bacaan);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ViewPager mViewPager = (HackyViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new AdapterGambarPager());

I did find that custom View must be declared with the package name like this problem i've found: Creating custom button class in Android

so I changed the xml ViewPager to my package name, but still not working. Any clue?

edit: Here's the log

06-14 21:01:05.901: E/AndroidRuntime(28345): FATAL EXCEPTION: main
06-14 21:01:05.901: E/AndroidRuntime(28345): java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.co.senab.photoview.sample/uk.co.senab.photoview.sample.ViewPagerActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class uk.co.senab.photoview.sample.HackyViewPager
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.os.Looper.loop(Looper.java:137)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.app.ActivityThread.main(ActivityThread.java:4448)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at java.lang.reflect.Method.invokeNative(Native Method)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at java.lang.reflect.Method.invoke(Method.java:511)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at dalvik.system.NativeStart.main(Native Method)
06-14 21:01:05.901: E/AndroidRuntime(28345): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class uk.co.senab.photoview.sample.HackyViewPager
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.view.LayoutInflater.createView(LayoutInflater.java:589)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.app.Activity.setContentView(Activity.java:1835)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at uk.co.senab.photoview.sample.ViewPagerActivity.onCreate(ViewPagerActivity.java:34)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.app.Activity.performCreate(Activity.java:4465)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
06-14 21:01:05.901: E/AndroidRuntime(28345):    ... 11 more
06-14 21:01:05.901: E/AndroidRuntime(28345): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
06-14 21:01:05.901: E/AndroidRuntime(28345):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at java.lang.Class.getConstructor(Class.java:431)
06-14 21:01:05.901: E/AndroidRuntime(28345):    at android.view.LayoutInflater.createView(LayoutInflater.java:561)
06-14 21:01:05.901: E/AndroidRuntime(28345):    ... 22 more

Here's HackyViewPager.java:

package uk.co.senab.photoview.sample;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;

/**
 * Hacky fix for Issue #4 and
 * http://code.google.com/p/android/issues/detail?id=18990
 * 
 * ScaleGestureDetector seems to mess up the touch events, which means that
 * ViewGroups which make use of onInterceptTouchEvent throw a lot of
 * IllegalArgumentException: pointerIndex out of range.
 * 
 * There's not much I can do in my code for now, but we can mask the result by
 * just catching the problem and ignoring it.
 * 
 * @author Chris Banes
 */
public class HackyViewPager extends ViewPager {

    public HackyViewPager(Context context) {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        try {
            return super.onInterceptTouchEvent(ev);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return false;
        }
    }

}
like image 875
Victorio Pui Avatar asked Jun 14 '13 13:06

Victorio Pui


People also ask

How to Set ViewPager in android?

Android ViewPager widget is found in the support library and it allows the user to swipe left or right to see an entirely new screen. Today we're implementing a ViewPager by using Views and PagerAdapter. Though we can implement the same using Fragments too, but we'll discuss that in a later tutorial.


2 Answers

 <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="7" />

if you want to cast to your cusmo ViewPager you should change the xml to

 <com.yourpackage.HackyViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="7" />

you missed to declare the following constructor

public HackyViewPager(Context context, AttributeSet attrs) {
   super(context, attrs);
}
like image 132
Blackbelt Avatar answered Oct 24 '22 12:10

Blackbelt


Since you are using a custom ViewPager and not the one in the support library you need to change your xml

change

<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="7" />

to

<com.package.HackyViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="7" />

of course use the path to the proper ViewPager.

like image 45
mike.b93 Avatar answered Oct 24 '22 13:10

mike.b93