15

我正在使用来自 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());

我确实发现必须使用包名称声明自定义视图,就像我发现的这个问题:在 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

4 回答 4

26
 <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);
}
于 2013-06-14T13:57:19.230 回答
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 的路径。

于 2013-06-14T14:00:16.437 回答
1

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

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

public HackyViewPager(Context context) {
   super(context);
}
于 2013-07-26T17:31:13.967 回答
0

也许,现在回答这个问题为时已晚,我也遇到了同样的问题。Can't cast or can't inflate 这个错误是因为我将 viewpager 扩展为显示这个 viewpager 的活动的内部类,所以我所做的是,我为那个 customViewPager 创建了一个单独的类,然后问题就解决了。

于 2018-02-22T07:52:17.163 回答