0

I am trying to write program that transforms a picture to black-white picture. I encountered with a problem and searched for the same issues but i didn't find a proper solution for me. Here is the code:

public class MainActivity extends AppCompatActivity {

ImageView image;
Drawable drawable;
Bitmap bitmap;

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

    image = (ImageView) findViewById(R.id.imageView);

    bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); **// Here is line that error occur**

    image.setImageBitmap(converter(bitmap));

}

public Bitmap converter(Bitmap first) {
     Bitmap end = Bitmap.createBitmap(first.getWidth(),
             first.getHeight(),
             first.getConfig());

Stacktrace:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
                      at com.example.murat.giveeffect.MainActivity.onCreate(MainActivity.java:26)
3
  • Does your imageView has an image already loaded?. Commented Dec 21, 2017 at 20:21
  • Yes, it isnt empty Commented Dec 21, 2017 at 20:23
  • Try this image.setDrawingCacheEnabled(true); bitmap = image.getDrawingCache(); instead bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); Commented Dec 21, 2017 at 20:26

1 Answer 1

1

You set your ImageView's bitmap drawable to a background attribute

android:background="@drawable/clip"

instead of src.

So change your resource file (activity_main.xml) by replacing background attribute to android:src:

android:src="@drawable/clip"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.