user3211973
user3211973

Reputation: 1

Trouble with android

I have one when draw in ImageView. When draw from onCreate() all OK, but when I draw by pressing the button (function "qwe") produces an error. What is wrong?

    package com.example.asd;

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageView;

    public class MainActivity extends Activity {
        Bitmap myBitmap;
        Canvas myCanvas;
        ImageView myImageView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            config();
        }
        public void config()
        {
          myBitmap=Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
              myCanvas=new Canvas(myBitmap);
              myImageView =(ImageView)findViewById(R.id.imageView1);
            for(int i=0;i<200;i++)
                for(int j=0;j<200;j++)
                    myBitmap.setPixel(i, j, 0xffffff00);
            myImageView.setImageBitmap(myBitmap);
            Paint myPaint=new Paint();
            myPaint.setColor(0xff000000);
            myCanvas.drawLine(10, 10, 190, 190, myPaint);
        }
        public void qwe(View v)
        {   Paint myPaint=null;
            myPaint.setColor(0xff000000);
            myCanvas.drawLine(190, 10, 190, 10, myPaint);
            myImageView.draw(myCanvas);
        }
    }

Upvotes: 0

Views: 52

Answers (1)

nano_nano
nano_nano

Reputation: 12523

you get a NullPointerException here:

Paint myPaint=null;
        myPaint.setColor(0xff000000);

you have to initiate myPaint before using it!

Paint myPaint= new Paint();

Upvotes: 4

Related Questions