Reputation: 271
I have the following code, to set an image and draw text on top of the image. However I cant seem to get the code to work, my program exits with a NPE (see CATLOG). It seems that the line setImageBitmap() in createImage method is causing this, though I cant seem to figure out why.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
Intent intent = getIntent();
int position = intent.getIntExtra("position", 0);
Log.w("myApp", "" + position);
final ImageView iv_ttx = (ImageView) findViewById(R.id.iv_ttx);
final int[] imgSizeIds = new int[] { R.drawable.img1,
R.drawable.img2, R.drawable.img3 };
// Create Paint Object
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLUE);
paint.setTextSize(75);
// Create or Load Bitmap
iv_ttx.setImageResource(imgSizeIds[position]);
originalBitmap = BitmapFactory.decodeResource(getResources(),
imgSizeIds[position]);
image = originalBitmap.copy(Bitmap.Config.RGB_565, true);
iv_ttx.setImageBitmap(image);
et_top = (EditText) findViewById(R.id.top_txt);
Button btntop = (Button) findViewById(R.id.btntop);
// Set OnClickListeners
btntop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String user_text = et_top.getText().toString();
createImage(user_text);
}
});
public Bitmap createImage(String user_text) {
// canvas object with bitmap image as constructor
Canvas canvas = new Canvas(image);
canvas.drawText("" + user_text, 400, 400, paint);
iv_ttx.setImageBitmap(image);
return image;
}
Now for the interesting part, if I delete iv_ttx.setImageBitmap(image) line, the text seems to appear ontop of the image around 30s after I've clicked the top button or if I keep persistently clicking it, any ideas?
CATLOG:
09-06 22:14:39.197: E/AndroidRuntime(21242): FATAL EXCEPTION: main
09-06 22:14:39.197: E/AndroidRuntime(21242): java.lang.NullPointerException
09-06 22:14:39.197: E/AndroidRuntime(21242): at com.meme.hdmeme.MemeEditor.createImage(MemeEditor.java:72)
09-06 22:14:39.197: E/AndroidRuntime(21242): at com.meme.hdmeme.MemeEditor$1.onClick(MemeEditor.java:63)
09-06 22:14:39.197: E/AndroidRuntime(21242): at android.view.View.performClick(View.java:4204)
09-06 22:14:39.197: E/AndroidRuntime(21242): at android.view.View$PerformClick.run(View.java:17355)
09-06 22:14:39.197: E/AndroidRuntime(21242): at android.os.Handler.handleCallback(Handler.java:725)
09-06 22:14:39.197: E/AndroidRuntime(21242): at android.os.Handler.dispatchMessage(Handler.java:92)
09-06 22:14:39.197: E/AndroidRuntime(21242): at android.os.Looper.loop(Looper.java:137)
09-06 22:14:39.197: E/AndroidRuntime(21242): at android.app.ActivityThread.main(ActivityThread.java:5214)
09-06 22:14:39.197: E/AndroidRuntime(21242): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 22:14:39.197: E/AndroidRuntime(21242): at java.lang.reflect.Method.invoke(Method.java:511)
09-06 22:14:39.197: E/AndroidRuntime(21242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
09-06 22:14:39.197: E/AndroidRuntime(21242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
09-06 22:14:39.197: E/AndroidRuntime(21242): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 2868
Reputation: 133560
Declare ImageView iv_ttx
as a class member.
ImageView iv_ttx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
iv_ttx = (ImageView) findViewById(R.id.iv_ttx);
Upvotes: 1
Reputation: 1250
final ImageView iv_ttx = (ImageView) findViewById(R.id.iv_ttx);
Is declared in onCreate so iv_ttx
can be null in other function becouse it's declared but not initialized(is it?). Remove final ImageView
and let iv_ttx
be member of class.
Upvotes: 1