1

私のアプリケーションでは、Chrisbanes Github: https://github.com/chrisbanes/PhotoViewを使用しており、LinearLayout で ViewPager を作成しようとしましたが、ViewPager を HackyViewPager にキャストするとエラーが発生します。質問はこれに非常に似ています:カスタム ViewPager クラスにキャストできません

しかし、私は解決策を理解できません。私の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