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)