Android pick image from Gallery NullPointerException?-Collection of common programming errors
I am currently trying to create an App, which allows the user to pic an image from gallery and upload it to a server. But i’m still stuck in picking an image. I followed a tutorial exactly, but when launching the activity i get a NullPointer.
Do you guys see any errors?
Thanks in advance!
Heres the code;
package de.arvidg.exampleactionbartabs;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class UploadPicture extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.choosepic2);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.chosenPicView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
LogCat:
03-17 10:56:35.470: W/dalvikvm(12169): threadid=1: thread exiting with uncaught exception (group=0x41e64300)
03-17 10:56:35.480: E/AndroidRuntime(12169): FATAL EXCEPTION: main
03-17 10:56:35.480: E/AndroidRuntime(12169): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.arvidg.exampleactionbartabs/de.arvidg.exampleactionbartabs.UploadPicture}: java.lang.NullPointerException
03-17 10:56:35.480: E/AndroidRuntime(12169): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
03-17 10:56:35.480: E/AndroidRuntime(12169): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2215)
03-17 10:56:35.480: E/AndroidRuntime(12169): at android.app.ActivityThread.access$600(ActivityThread.java:145)
03-17 10:56:35.480: E/AndroidRuntime(12169): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
03-17 10:56:35.480: E/AndroidRuntime(12169): at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 10:56:35.480: E/AndroidRuntime(12169): at android.os.Looper.loop(Looper.java:137)
03-17 10:56:35.480: E/AndroidRuntime(12169): at android.app.ActivityThread.main(ActivityThread.java:4978)
03-17 10:56:35.480: E/AndroidRuntime(12169): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 10:56:35.480: E/AndroidRuntime(12169): at java.lang.reflect.Method.invoke(Method.java:511)
03-17 10:56:35.480: E/AndroidRuntime(12169): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-17 10:56:35.480: E/AndroidRuntime(12169): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
03-17 10:56:35.480: E/AndroidRuntime(12169): at dalvik.system.NativeStart.main(Native Method)
03-17 10:56:35.480: E/AndroidRuntime(12169): Caused by: java.lang.NullPointerException
03-17 10:56:35.480: E/AndroidRuntime(12169): at de.arvidg.exampleactionbartabs.UploadPicture.onCreate(UploadPicture.java:34)
03-17 10:56:35.480: E/AndroidRuntime(12169): at android.app.Activity.performCreate(Activity.java:5008)
03-17 10:56:35.480: E/AndroidRuntime(12169): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-17 10:56:35.480: E/AndroidRuntime(12169): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-17 10:56:35.480: E/AndroidRuntime(12169): ... 11 more
main.xml
Line 34 is the last bracket of that statement
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
-
just change
Button buttonLoadImage = (Button) findViewById(R.id.choosepic2);
to
Button buttonLoadImage = (Button) findViewById(R.id.selectpic);
at some point in your code you have changed the id of your button in the layout file and didn’t reflect that change on the activity code.
always double check your ids.
-
Your mistake is i think in front of you. You have given wrong id of the
Button
andImageView
also.Initialize your button with the id which you have given in your layout as below: Define your button id as
R.id.selectpic
.Button buttonLoadImage = (Button) findViewById(R.id.selectpic);
And your
ImageView
id asR.id.imageView1
besidesR.id.chosenPicView
as below:ImageView imageView = (ImageView) findViewById(R.id.imageView1);
And then try out.
Originally posted 2013-11-16 20:49:04.