使用SwipeBack无法正常工作的PhotoView(用于缩放)

时间:2015-11-05 09:19:57

标签: android android-layout android-activity image-zoom

我有一项活动,其中我必须显示具有缩放到缩放功能的图像,我需要关闭活动,将其拖放到Facebook应用程序中。我使用Chris Banes的PhotoView进行图像缩放,使用SwipeBack通过向下拖动来关闭活动。这是我的活动:

public class AlternateFullImageActivity extends SwipeBackActivity {
    String photoId;
    String token;
    TinyDB tinyDB;
    RestAdapter restAdapter;
    @Bind(R.id.zoomable)
    ImageView image;
    @Bind(R.id.swipe_layout)
    SwipeBackLayout swipeBackLayout;
    PhotoViewAttacher mAttacher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alternate_full_image);
        setDragEdge(SwipeBackLayout.DragEdge.TOP);

        ButterKnife.bind(this);

        tinyDB = new TinyDB(this);
        token = tinyDB.getString("Galleri5 Access Token");

        Intent intent = getIntent();
        photoId = intent.getExtras().getString("ID");

        RequestInterceptor requestInterceptor = new RequestInterceptor() {
            @Override
            public void intercept(RequestFacade request) {
                request.addHeader("Accept", "application/json");
                request.addHeader("Authorization", "Token " + token);
            }
        };
        OkHttpClient okHttpClient = new OkHttpClient();
        restAdapter = new RestAdapter.Builder()
                .setClient(new OkClient(okHttpClient))
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(API)
                .setRequestInterceptor(requestInterceptor)
                .build();

        PhotoAPI photoAPI = restAdapter.create(PhotoAPI.class);
        photoAPI.getFeed(photoId, new Callback<ImageItem>() {
            @Override
            public void success(ImageItem imageItem, Response response) {
                Picasso.with(AlternateFullImageActivity.this)
                        .load(imageItem.getPhotoUrl())
                        .into(image);
                mAttacher = new PhotoViewAttacher(image);
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }

}

这是我的布局文件:

<com.liuguangqiang.swipeback.SwipeBackLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipe_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.galleri5.android.activities.AlternateFullImageActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

    <uk.co.senab.photoview.PhotoView
        android:id="@+id/zoomable"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" />

</RelativeLayout>

</com.liuguangqiang.swipeback.SwipeBackLayout>

当我运行它时,一切正常,但是一旦我缩小图像,我就会收到此错误:

11-05 14:23:37.125 3506-3506/com.galleri5.android E/InputEventReceiver: Exception dispatching input event.
11-05 14:23:37.126 3506-3506/com.galleri5.android E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.support.v4.widget.ViewDragHelper.shouldInterceptTouchEvent(ViewDragHelper.java:1014)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at com.liuguangqiang.swipeback.SwipeBackLayout.onInterceptTouchEvent(SwipeBackLayout.java:248)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2059)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2569)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1743)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.app.Activity.dispatchTouchEvent(Activity.java:2739)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2530)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.View.dispatchPointerEvent(View.java:8578)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4028)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3894)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3456)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3509)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3475)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3585)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3483)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3642)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3456)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3509)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3475)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3483)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3456)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5708)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5682)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5653)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5798)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.os.MessageQueue.nativePollOnce(Native Method)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.os.MessageQueue.next(MessageQueue.java:143)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.os.Looper.loop(Looper.java:122)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at android.app.ActivityThread.main(ActivityThread.java:5254)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at java.lang.reflect.Method.invoke(Native Method)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at java.lang.reflect.Method.invoke(Method.java:372)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
11-05 14:23:37.130 3506-3506/com.galleri5.android E/MessageQueue-JNI:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime: FATAL EXCEPTION: main
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime: Process: com.galleri5.android, PID: 3506
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.support.v4.widget.ViewDragHelper.shouldInterceptTouchEvent(ViewDragHelper.java:1014)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at com.liuguangqiang.swipeback.SwipeBackLayout.onInterceptTouchEvent(SwipeBackLayout.java:248)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2059)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2569)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1743)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.app.Activity.dispatchTouchEvent(Activity.java:2739)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2530)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.View.dispatchPointerEvent(View.java:8578)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4028)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3894)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3456)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3509)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3475)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3585)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3483)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3642)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3456)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3509)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3475)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3483)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3456)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5708)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5682)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5653)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5798)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.os.MessageQueue.nativePollOnce(Native Method)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.os.MessageQueue.next(MessageQueue.java:143)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:122)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
11-05 14:23:37.172 3506-3506/com.galleri5.android E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

之后我读到了关于PhotoView的内容,并且按照github页面的说明,我尝试为SwipeBackLayout创建一个Hacky Layout。这是代码:

import com.liuguangqiang.swipeback.SwipeBackLayout;

public class HackySwipeBackLayout extends SwipeBackLayout {
    public HackySwipeBackLayout(Context context) {
        super(context);
    }

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

然后我使用新的HackySwipebackLayout替换了早期的布局文件。

但之后,当我运行应用程序并点击图片进入上述活动时,我收到此错误:

11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime: FATAL EXCEPTION: main
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime: Process: com.galleri5.android, PID: 6709
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.galleri5.android/com.galleri5.android.activities.AlternateFullImageActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class com.galleri5.android.utils.HackySwipeBackLayout
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:147)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class com.galleri5.android.utils.HackySwipeBackLayout
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.view.LayoutInflater.createView(LayoutInflater.java:616)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at com.liuguangqiang.swipeback.SwipeBackActivity.setContentView(SwipeBackActivity.java:21)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at com.galleri5.android.activities.AlternateFullImageActivity.onCreate(AlternateFullImageActivity.java:48)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5933)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:147) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:  Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at java.lang.Class.getConstructor(Class.java:531)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at java.lang.Class.getConstructor(Class.java:495)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.view.LayoutInflater.createView(LayoutInflater.java:580)
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at com.liuguangqiang.swipeback.SwipeBackActivity.setContentView(SwipeBackActivity.java:21) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at com.galleri5.android.activities.AlternateFullImageActivity.onCreate(AlternateFullImageActivity.java:48) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5933) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:147) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) 
11-05 14:28:22.245 6709-6709/com.galleri5.android E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) 

可能的原因是什么以及如何使其发挥作用?我真的需要它来工作。感谢。

1 个答案:

答案 0 :(得分:2)

java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]

您的HackySwipeBackLayout缺少构造函数:

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

如果你仔细查看HackyViewPager,你会注意到它也在那里。事实上,任何需要支持xml膨胀的视图都应该定义这个构造函数。您可能会发现documentation on how to create custom views也很有帮助。

编辑:因为您的目的是捕获抛出的异常,所以请务必相应地捕获ArrayIndexOutOfBoundsException

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

这应该可以阻止您的应用在此特定异常上崩溃,但当然不是“修复”问题。对于导致ArrayIndexOutOfBoundsException被抛出的基础问题。它可能会也可能不会导致所需的行为。无论哪种方式,考虑使用SwipeBack repo的维护者打开ticket