无法强制转换为自定义ViewPager类

时间:2013-06-14 13:55:15

标签: android android-viewpager custom-view

我正在使用来自Chrisbanes Github的这个来源:https://github.com/chrisbanes/PhotoView并尝试使用LinearLayout创建ViewPager但是在将ViewPager强制转换为HackyViewPager时出现错误 这是我的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>
在ViewPagerActivity.java上的

,我改变了这个:

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

到此:

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

我确实发现必须使用包名称声明自定义View,就像我找到的这个问题一样:Creating custom button class in Android

所以我将xml ViewPager更改为我的包名,但仍然无效。任何线索?

编辑: 这是日志

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

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

}

4 个答案:

答案 0 :(得分:25)

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

如果您想要投射到您的cusmo ViewPager,您应该将xml更改为

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

您错过了声明以下构造函数

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

答案 1 :(得分:4)

由于您使用的是自定义ViewPager而不是支持库中的ViewPager,因此您需要更改xml

变化

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

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

当然使用正确的ViewPager的路径。

答案 2 :(得分:1)

您必须在班级中调用超级构造函数。您是否尝试过自定义ViewPager类中的两个构造函数?等,

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

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

答案 3 :(得分:0)

也许,回答这个问题为时已晚,我已经遇到了同样的问题。这个错误无法投射或无法膨胀是因为我将viewpager扩展为显示此事件的活动的内部类viewpager,所以我做的是,我为该customViewPager创建了一个单独的类,然后解决了问题。