質問

I am trying to select a picture from the pictures allready on the device. I had this code working in a previous app but for some reason it just won't work. My onActivityResult gives a nullpointer exception when trying to set the selected image to an imageview, so i guess the Bitmap is null. I tried numerous tutorials and used that code, rewrote everything but everytime the same problem occurs. I have tried multiple devices, running android 4.4 and android 4.2.2 (so its not just a kitkat problem).

my code:

public class Iconize extends Activity{


public ImageView ivMain;
LinearLayout topscrollitems;
Bitmap bitmap;
int REQUEST_CODE=1;
FrameLayout fl ;
Context c;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_iconize);
    c= getApplicationContext();
    topscrollitems = (LinearLayout) this.findViewById(R.id.ll_topscrollview);
    fl= (FrameLayout)this.findViewById(R.id.framelayoutholder);
    //some stuff not related to this question
    //....//
    //

    final Context c = getApplicationContext();
    pickImageFromDevice();

}



@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    System.out.println("STOP");
}

public void pickImageFromDevice() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(intent, REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
        try {
            if (bitmap != null) {
                bitmap.recycle();
            }
            InputStream stream = getContentResolver().openInputStream(
                    data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
            ivMain.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e){
            e.printStackTrace();
        }
    super.onActivityResult(requestCode, resultCode, data);
}


}

and the stacktrace:

04-21 10:28:18.499: W/System.err(25951): java.lang.NullPointerException
04-21 10:28:18.519: W/System.err(25951):    at com.sa.iconize.Iconize.onActivityResult(Iconize.java:114)
04-21 10:28:18.519: W/System.err(25951):    at android.app.Activity.dispatchActivityResult(Activity.java:5423)
04-21 10:28:18.519: W/System.err(25951):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
04-21 10:28:18.519: W/System.err(25951):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
04-21 10:28:18.519: W/System.err(25951):    at android.app.ActivityThread.access$1300(ActivityThread.java:135)
04-21 10:28:18.519: W/System.err(25951):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
04-21 10:28:18.519: W/System.err(25951):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-21 10:28:18.519: W/System.err(25951):    at android.os.Looper.loop(Looper.java:136)
04-21 10:28:18.519: W/System.err(25951):    at android.app.ActivityThread.main(ActivityThread.java:5017)
04-21 10:28:18.519: W/System.err(25951):    at java.lang.reflect.Method.invokeNative(Native Method)
04-21 10:28:18.519: W/System.err(25951):    at java.lang.reflect.Method.invoke(Method.java:515)
04-21 10:28:18.519: W/System.err(25951):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-21 10:28:18.519: W/System.err(25951):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-21 10:28:18.519: W/System.err(25951):    at dalvik.system.NativeStart.main(Native Method)

where the line where the error occurs is:

ivMain.setImageBitmap(bitmap);

I think I am missing something really obvious, because this should not be to hard and I've had this working on previous projects before...

役に立ちましたか?

解決

Are you sure ivMain is properly initialized?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top