几次滑动后ViewPager停止查看布局(NullPointerException)

时间:2014-12-17 18:48:41

标签: android nullpointerexception android-viewpager

所以现在我遇到了这个app应用程序工作的问题但是在通过刷卡进行一些视图更改后,它会因NullPointerException而崩溃。首先 - 在onTouchListener上有一个问题。我设法让它工作,但现在我需要设法让它不崩溃(多么明显!)。

据我所知 - 找到R.id.buttons有一个问题,它基本上是ImageView,是.xml文件的一个重要组成部分(它的设置并没有在整个应用程序中改变),但经过几次刷卡我得到NullPointerException这很奇怪。

这是代码(以及后来的LogCat日志):

public class ViewPagerAdapter extends PagerAdapter{
    // Declare Variables
    Context context;
    int[] gim;
    int[] sounds;
    ArrayList<String> title;
    int[] description;
    LayoutInflater inflater;
    View itemView;

    public ViewPagerAdapter(Context context, int[] gim, ArrayList<String> title, 
            int[] description) {
        this.context = context;
        this.gim = gim;
        this.title = title;
        this.description = description; 
    }

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

    public int getItemPosition(Object object){
        return POSITION_NONE;
   }

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

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        // Declare Variables
        final ImageView imggun;  

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemView = inflater.inflate(R.layout.viewpager_item, container,
                false);
        FrameLayout layout = (FrameLayout) itemView.findViewById(R.id.fl1);
        layout.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent ev) {
                final int action = ev.getAction();
                final int evX = (int) ev.getX();
                final int evY = (int) ev.getY();
                switch (action) {
                case MotionEvent.ACTION_DOWN:

                    int touchColor = getHotspotColor(R.id.buttons, evX, evY);
                    ColorTool ct = new ColorTool(); //line 83
                    int tolerance = 25;

                    // "Go to the party" button
                    if (ct.closeMatch(Color.RED, touchColor, tolerance)) {
                        // TODO
                        Log.i("ABC", "Works");                  
                    }
                    // "Go home" button
                    if (ct.closeMatch(Color.BLUE, touchColor, tolerance)) {
                        // TODO
                        Log.i("ABC", "That one too");
                    }
                    break;

                case MotionEvent.ACTION_UP:
                    // TODO
                    break;
                }
                return true;
            }
        });

        // Locate the ImageView in viewpager_item.xml
        imggun = (ImageView) itemView.findViewById(R.id.gun);
        // Capture position and set to the ImageView
        imggun.setImageResource(gim[position]);


        // Add viewpager_item.xml to ViewPager
        ((ViewPager) container).addView(itemView);                        

        return itemView;
    }

    protected Context getApplicationContext() {
        return null;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((FrameLayout) object);
        FrameLayout layout = (FrameLayout) object;       
        ((ViewPager) container).removeView(layout);
    }

    public int getHotspotColor(int hotspotId, int x, int y) {
        ImageView img = (ImageView) itemView.findViewById(hotspotId);
        img.setDrawingCacheEnabled(true);
        Bitmap hotspots = Bitmap.createBitmap(img.getDrawingCache()); 
        img.setDrawingCacheEnabled(false); //line 152
        return hotspots.getPixel(x, y);
    }

    public class ColorTool {
        public boolean closeMatch(int color1, int color2, int tolerance) {
            if ((int) Math.abs(Color.red(color1) - Color.red(color2)) > tolerance)
                return false;
            if ((int) Math.abs(Color.green(color1) - Color.green(color2)) > tolerance)
                return false;
            if ((int) Math.abs(Color.blue(color1) - Color.blue(color2)) > tolerance)
                return false;
            return true;
        } // end match
    }
}

LogCat日志:

12-18 07:30:14.219: E/InputEventReceiver(20187): Exception dispatching input event.
12-18 07:30:14.219: E/MessageQueue-JNI(20187): Exception in MessageQueue callback: handleReceiveCallback
12-18 07:30:14.228: E/MessageQueue-JNI(20187): java.lang.NullPointerException
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.graphics.Bitmap.createBitmap(Bitmap.java:639)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at pl.dajsiezlapac.apka.ViewPagerAdapter.getHotspotColor(ViewPagerAdapter.java:154)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at pl.dajsiezlapac.apka.ViewPagerAdapter$1.onTouch(ViewPagerAdapter.java:78)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.View.dispatchTouchEvent(View.java:7731)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2212)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.app.Activity.dispatchTouchEvent(Activity.java:2466)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.View.dispatchPointerEvent(View.java:7916)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4023)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3902)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3479)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3479)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5657)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5588)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5559)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5737)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.os.MessageQueue.nativePollOnce(Native Method)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.os.MessageQueue.next(MessageQueue.java:138)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.os.Looper.loop(Looper.java:123)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at android.app.ActivityThread.main(ActivityThread.java:5086)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at java.lang.reflect.Method.invokeNative(Native Method)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at java.lang.reflect.Method.invoke(Method.java:515)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-18 07:30:14.228: E/MessageQueue-JNI(20187):  at dalvik.system.NativeStart.main(Native Method)
12-18 07:30:14.230: D/AndroidRuntime(20187): Shutting down VM
12-18 07:30:14.230: W/dalvikvm(20187): threadid=1: thread exiting with uncaught exception (group=0x41704d40)
12-18 07:30:14.238: E/AndroidRuntime(20187): FATAL EXCEPTION: main
12-18 07:30:14.238: E/AndroidRuntime(20187): Process: pl.dajsiezlapac.apka, PID: 20187
12-18 07:30:14.238: E/AndroidRuntime(20187): java.lang.NullPointerException
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.graphics.Bitmap.createBitmap(Bitmap.java:639)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at pl.dajsiezlapac.apka.ViewPagerAdapter.getHotspotColor(ViewPagerAdapter.java:154)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at pl.dajsiezlapac.apka.ViewPagerAdapter$1.onTouch(ViewPagerAdapter.java:78)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.View.dispatchTouchEvent(View.java:7731)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2212)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.app.Activity.dispatchTouchEvent(Activity.java:2466)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.View.dispatchPointerEvent(View.java:7916)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4023)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3902)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3479)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3479)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5657)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5588)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5559)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5737)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.os.MessageQueue.nativePollOnce(Native Method)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.os.MessageQueue.next(MessageQueue.java:138)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.os.Looper.loop(Looper.java:123)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at android.app.ActivityThread.main(ActivityThread.java:5086)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at java.lang.reflect.Method.invokeNative(Native Method)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at java.lang.reflect.Method.invoke(Method.java:515)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-18 07:30:14.238: E/AndroidRuntime(20187):    at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案