扩展ViewPager无法正常工作

时间:2018-01-12 10:48:21

标签: java android image-zoom

我无法解决我的问题。

我试图扩展ViewPager类,我做了HackyProblematicViewGroup,因为在缩小图像时我有bug,所以我需要让该类捕获并忽略异常。

我的课程是:

public class HackyProblematicViewGroup extends ViewPager {

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

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        try {
            return super.onInterceptTouchEvent(ev);
        } catch (IllegalArgumentException e) {
            //uncomment if you really want to see these errors
            //e.printStackTrace();
            return false;
        }
    }
}

在我的活动课上,我做了一些改动:

HackyProblematicViewGroup viewPager;
static String s;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_katalog_opend);

    viewPager = (HackyProblematicViewGroup) findViewById(R.id.viewPager);

    s = getIntent().getExtras().getString("KatalogID");

    ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
    viewPager.setAdapter(viewPagerAdapter);
}

这是我的xml:

<com.example.miki.katalozi1.HackyProblematicViewGrou
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="bottom"
tools:layout_editor_absoluteY="8dp"

tools:layout_editor_absoluteX="8dp">

<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:id="@+id/tab_layout"
    android:layout_height="wrap_content"
    app:tabBackground="@drawable/tab_selector"
    app:tabGravity="center"
    android:layout_gravity="bottom"
    app:tabIndicatorHeight="0dp"/>
</com.example.miki.katalozi1.HackyProblematicViewGrou>

毕竟我有下一个错误:

Process: com.example.miki.katalozi1, PID: 25034
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.miki.katalozi1/com.example.miki.katalozi1.KatalogOpend}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class com.example.miki.katalozi1.HackyProblematicViewGrou
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
    at android.app.ActivityThread.-wrap14(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
 Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class com.example.miki.katalozi1.HackyProblematicViewGrou
 Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class com.example.miki.katalozi1.HackyProblematicViewGrou
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.miki.katalozi1.HackyProblematicViewGrou" on path: DexPathList[[zip file "/data/app/com.example.miki.katalozi1-2/base.apk", zip file "/data/app/com.example.miki.katalozi1-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.miki.katalozi1-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.miki.katalozi1-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.miki.katalozi1-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.miki.katalozi1-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.miki.katalozi1-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.miki.katalozi1-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.miki.katalozi1-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.miki.katalozi1-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.miki.katalozi1-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.miki.katalozi1-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.miki.katalozi1-2/lib/arm64, /system/lib64, /vendor/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.view.LayoutInflater.createView(LayoutInflater.java:616)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:378)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
    at com.example.miki.katalozi1.KatalogOpend.onCreate(KatalogOpend.java:24)
    at android.app.Activity.performCreate(Activity.java:6955)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
    at android.app.ActivityThread.-wrap14(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
    at android.os.Handler.dispatchMessage(Handler.java:102)
01-12 11:30:40.445 25034-25034/com.example.miki.katalozi1 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

2 个答案:

答案 0 :(得分:0)

请使用此

public class NonSwipeableViewPager extends ViewPager {

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

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

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    // Never allow swiping to switch between pages
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // Never allow swiping to switch between pages
    return false;
}
}

答案 1 :(得分:0)

改变这一点 - :

<com.example.miki.katalozi1.HackyProblematicViewGrou
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="bottom"
tools:layout_editor_absoluteY="8dp"

tools:layout_editor_absoluteX="8dp">

要 - :

<com.example.miki.katalozi1.HackyProblematicViewGroup
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="bottom"
tools:layout_editor_absoluteY="8dp"

tools:layout_editor_absoluteX="8dp">

// HackyProblematicViewGroup这是你的班级名称