1

在我的应用程序中,我使用的是 Chrisbanes Github:https ://github.com/chrisbanes/PhotoView并尝试在 LinearLayout 中制作 ViewPager,但在将 ViewPager 转换为 HackyViewPager 时出现错误。问题与此非常相似:Cannot cast to custom ViewPager class

但我无法理解解决方案。我的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:gravity="center_vertical"
android:orientation="vertical" >
<com.pack.HackyViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="7" />

我的活动是:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.photo_gallery_layout);

    mViewPager = new HackyViewPager(this, null);

    //setContentView(mViewPager);
    mViewPager=(HackyViewPager) findViewById(R.id.pager);

    mViewPager.setAdapter(new SamplePagerAdapter());
}

static class SamplePagerAdapter extends PagerAdapter {

    private static int[] sDrawables = { R.drawable.bg, R.drawable.bg,
            R.drawable.bg, R.drawable.bg, R.drawable.bg, R.drawable.bg };

    @Override
    public int getCount() {
        return sDrawables.length;
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        PhotoView photoView = new PhotoView(container.getContext());
        photoView.setImageResource(sDrawables[position]);

        // Now just add PhotoView to ViewPager and return it
        container.addView(photoView, LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);

        return photoView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

}

HackyViewPager 是:

public class HackyViewPager extends ViewPager {

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

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

错误是(在我设置适配器时的活动中):

07-09 15:07:39.292: E/AndroidRuntime(16711): FATAL EXCEPTION: main
07-09 15:07:39.292: E/AndroidRuntime(16711): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pack./com.pack.PhotoGalleryActivity}: java.lang.NullPointerException
07-09 15:07:39.292: E/AndroidRuntime(16711):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
07-09 15:07:39.292: E/AndroidRuntime(16711):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
07-09 15:07:39.292: E/AndroidRuntime(16711):    at android.app.ActivityThread.access$700(ActivityThread.java:152)
07-09 15:07:39.292: E/AndroidRuntime(16711):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
07-09 15:07:39.292: E/AndroidRuntime(16711):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 15:07:39.292: E/AndroidRuntime(16711):    at android.os.Looper.loop(Looper.java:137)
07-09 15:07:39.292: E/AndroidRuntime(16711):    at android.app.ActivityThread.main(ActivityThread.java:5328)
07-09 15:07:39.292: E/AndroidRuntime(16711):    at java.lang.reflect.Method.invokeNative(Native Method)
07-09 15:07:39.292: E/AndroidRuntime(16711):    at java.lang.reflect.Method.invoke(Method.java:511)
07-09 15:07:39.292: E/AndroidRuntime(16711):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-09 15:07:39.292: E/AndroidRuntime(16711):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-09 15:07:39.292: E/AndroidRuntime(16711):    at dalvik.system.NativeStart.main(Native Method)
07-09 15:07:39.292: E/AndroidRuntime(16711): Caused by: java.lang.NullPointerException
07-09 15:07:39.292: E/AndroidRuntime(16711):    at it.reply.syskoplan.pirelliapp.PhotoGalleryActivity.onCreate(PhotoGalleryActivity.java:29)
07-09 15:07:39.292: E/AndroidRuntime(16711):    at android.app.Activity.performCreate(Activity.java:5250)
07-09 15:07:39.292: E/AndroidRuntime(16711):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
07-09 15:07:39.292: E/AndroidRuntime(16711):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-09 15:07:39.292: E/AndroidRuntime(16711):    ... 11 more
4

0 回答 0