擴展Viewpage 類,結果得到java.lang.RuntimeException
。
Unable to start activity ComponentInfo{com.pindak.sarito/com.pindak.sarito.ui.ws.MagazineReaderActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class com.pindak.sarito.util.HackyViewPager
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<com.pindak.sarito.util.HackyViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/magazinepager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
onCreate的部分代碼:
HackyViewPager pager = (HackyViewPager) findViewById(R.id.magazinepager);
在com.pindak.sarito.util中的HackyViewPager.java
package com.pindak.sarito.util;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
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;
}
}
}
不知道為什麼會出現這個錯誤?
你有沒有試過添加兩者都匹配的構建器?
public HackyViewPager(Context context) {
super(context);
}
public HackyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}