Reputation: 21
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)
Upvotes: 0
Views: 836
Reputation: 301
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"
Upvotes: 1