Android:为什么抛出java.lang.IllegalStateException?

时间:2016-09-24 10:28:20

标签: java android illegalstateexception

我的这个应用程序有点问题! 这段代码:

package com.project.alpha_droid;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    Bitmap img;
    ImageView imagew;

    void fill(int x, int y, int c){
        if(img.getPixel(x,y)!=c){
            img.setPixel(x,y,c);
            if(img.getPixel(x+1,y)!=c){
                fill(x+1,y,c);
            }
            if(img.getPixel(x,y+1)!=c){
                fill(x,y+1,c);
            }
            if(img.getPixel(x-1,y)!=c){
                fill(x-1,y,c);
            }
            if(img.getPixel(x,y-1)!=c){
                fill(x,y-1,c);
            }
        }
    }


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

        imagew = (ImageView) findViewById(R.id.drawing);
        imagew.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d("Location", event.getX() + "x" + event.getY());
                img = ((BitmapDrawable)imagew.getDrawable()).getBitmap();
                fill(Math.round(event.getX()),Math.round(event.getY()), 0);
                imagew.setImageBitmap(img);
                return true;
            }
        });

    }
}

引发以下错误:

09-24 12:15:09.981 12764-12764/com.project.alpha_droid E/InputEventReceiver: Exception dispatching input event.
09-24 12:15:09.981 12764-12764/com.project.alpha_droid E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
09-24 12:15:09.981 12764-12764/com.project.alpha_droid E/MessageQueue-JNI: java.lang.IllegalStateException
                                                                        at android.graphics.Bitmap.setPixel(Bitmap.java:1746)
                                                                        at com.project.alpha_droid.MainActivity.fill(MainActivity.java:19)
                                                                        at com.project.alpha_droid.MainActivity$1.onTouch(MainActivity.java:47)
                                                                        at android.view.View.dispatchTouchEvent(View.java:9181)
                                                                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2691)
                                                                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2308)
                                                                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2691)
                                                                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2308)
                                                                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2691)
                                                                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2308)
                                                                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2691)
                                                                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2308)
                                                                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2691)
                                                                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2308)
                                                                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2691)
                                                                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2308)
                                                                        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2743)
                                                                        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1836)
                                                                        at android.app.Activity.dispatchTouchEvent(Activity.java:3043)
                                                                        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
                                                                        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2704)
                                                                        at android.view.View.dispatchPointerEvent(View.java:9397)
                                                                        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5082)
                                                                        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4899)
                                                                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4356)
                                                                        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4409)
                                                                        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4375)
                                                                        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4501)
                                                                        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4383)
                                                                        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4558)
                                                                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4356)
                                                                        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4409)
                                                                        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4375)
                                                                        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4383)
                                                                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4356)
                                                                        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6979)
                                                                        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6852)
                                                                        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6823)
                                                                        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7069)
                                                                        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                                                                        at android.os.MessageQueue.nativePollOnce(Native Method)
                                                                        at android.os.MessageQueue.next(MessageQueue.java:143)
                                                                        at android.os.Looper.loop(Looper.java:130)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6934)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

填充功能应该充当油漆中的桶,您可以用颜色填充您点击的空间。

1 个答案:

答案 0 :(得分:2)

您可以尝试将Bitmap复制到另一个可变的内容中。

   final Bitmap oldBitmap = ((BitmapDrawable) imagew.getDrawable()).getBitmap();

   //set the second param of copy to true so we get a **mutable** bitmap
   img = oldBitmap.copy(oldBitmap.getConfig, true);

   //recycle old. We don't need it anymore
   //oldBitmap.getBitmap().recycle()
   fill(Math.round(event.getX()),Math.round(event.getY()), 0);
   imagew.setImageBitmap(img);