0

Im currently using this tutorial http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/ for a gridview. What I'm attempting is to manipulate it so that it returns the selected picture as an activity result to my main activity so I can set it to an imageview and then be able to use the selected image later in some other task.

The problem I'm having is getting the result back to my main activity, I'm getting Uncaught handler errors. Yes I'm a noob so by all means point out my mistakes and any explanations as to where I went wrong are always helpful in learning.

My original call for startActivityForResult

Button.OnClickListener buttonClickListener4 = new Button.OnClickListener() {
  public void onClick(View ChoosePictureButton) {
    Intent intent = new Intent(PictureActivity.this, PicPicker2.class);
    startActivityForResult(intent, 2);

      } 

    };

The activity that should return the result on clicking of the image

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_layout);

    GridView gridView = (GridView) findViewById(R.id.grid_view);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));

    /**
     * On Click event for Single Gridview Item
     * */
    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            // Sending image id to PictureActivity
            Intent i = new Intent();
            // passing array index
            i.putExtra("id", position);
            setResult(Activity.RESULT_OK, i);
            finish();
        }
    });
}

}

Where I want my original activity to catch the result

  public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) {

             if(reqCode == 1) { for another activity result.... }           

              else if (reqCode == 2) {
                Intent i = getIntent();

                // Selected image id
                int position = i.getExtras().getInt("id");
                ImageAdapter imageAdapter = new ImageAdapter(this);
                ChosenImageView.setImageResource(imageAdapter.mThumbIds[position]);
              }
            }
        }   

Yes I probably butchered it, but it should give an idea of what I'm trying. My logcat is below.

10-21 16:41:30.839: E/AndroidRuntime(4978): Uncaught handler: thread main exiting due to uncaught exception
10-21 16:41:30.869: E/AndroidRuntime(4978): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { (has extras) }} to activity {jtek.industries.com/jtek.industries.com.PictureActivity}: java.lang.NullPointerException
10-21 16:41:30.869: E/AndroidRuntime(4978):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3329)
10-21 16:41:30.869: E/AndroidRuntime(4978):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3371)
10-21 16:41:30.869: E/AndroidRuntime(4978):     at android.app.ActivityThread.access$2700(ActivityThread.java:119)
10-21 16:41:30.869: E/AndroidRuntime(4978):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1893)
10-21 16:41:30.869: E/AndroidRuntime(4978):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 16:41:30.869: E/AndroidRuntime(4978):     at android.os.Looper.loop(Looper.java:123)
10-21 16:41:30.869: E/AndroidRuntime(4978):     at android.app.ActivityThread.main(ActivityThread.java:4363)
10-21 16:41:30.869: E/AndroidRuntime(4978):     at java.lang.reflect.Method.invokeNative(Native Method)
10-21 16:41:30.869: E/AndroidRuntime(4978):     at java.lang.reflect.Method.invoke(Method.java:521)
10-21 16:41:30.869: E/AndroidRuntime(4978):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-21 16:41:30.869: E/AndroidRuntime(4978):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-21 16:41:30.869: E/AndroidRuntime(4978):     at dalvik.system.NativeStart.main(Native Method)
10-21 16:41:30.869: E/AndroidRuntime(4978): Caused by: java.lang.NullPointerException
10-21 16:41:30.869: E/AndroidRuntime(4978):     at jtek.industries.com.PictureActivity.onActivityResult(PictureActivity.java:138)
10-21 16:41:30.869: E/AndroidRuntime(4978):     at android.app.Activity.dispatchActivityResult(Activity.java:3828)
10-21 16:41:30.869: E/AndroidRuntime(4978):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3325)
user1409172
  • 97
  • 1
  • 11
  • Whats in your `onActivityResult()`-method? – Lukas Knuth Oct 21 '12 at 22:11
  • Added the onActivityResult() its a switch statement based on the reqCodes and I left out the code for the first one as it works correctly and theres alot of it. – user1409172 Oct 21 '12 at 22:30
  • Is there any reason why you get the Intent with the `getIntent()`-method? I think you're getting the intent which does not hold the result-data, but the one which started the activity (which might be null). See if using the provided `Intent data`-parameter solves your problem. – Lukas Knuth Oct 21 '12 at 22:50
  • Was attempting to get the intent that was setup in the second activity Intent i = new Intent();. I think thats where my problem is as well but I cant quite figure out how to change it to the correct intent with the result-data. Any chance you can clarify? – user1409172 Oct 21 '12 at 23:06
  • Your the man @Lukas Knuth! Toss your last comment up as an answer so I can accept it, had to tinker for a bit but you set me in the right direction and got it working. – user1409172 Oct 21 '12 at 23:46

1 Answers1

1

In your onActivityResult()-method, Is there any reason why you get the Intent with the getIntent()-method?

I think you're getting the intent which does not hold the result-data, but the one which started the activity (which might be null). Therefore, in the line int position = i.getExtras().getInt("id"), i will be null and the NPE is thrown.

See if using the provided Intent data-parameter solves your problem.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111