Question

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

}
Was it helpful?

Solution

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

OTHER TIPS

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.

You must call super constructor in your class. Did you tried with both constructor in your custom ViewPager class? like,

public HackyViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}

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

Maybe,It's too late to answer this question, I've gone through the same problem .This error of Can't cast or can't inflate was because I was extending the viewpager as inner class of the activity that shows this viewpager ,so what I did ,was that I created a separate class for that customViewPager and then the problem solved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top