Reputation:
In Activity A, I have an image button used to intent Activity B.
Part of working code in Activity A
public void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Global.img=null;
Intent i = new Intent(A.this, B.class);
startActivityForResult(i, PROJECT_REQUEST_CODE);
}
});
}
public void onActivityResult(int requestCode,int resultCode, Intent data)
{
if(requestCode==PROJECT_REQUEST_CODE) {
c= data.getStringExtra("text");
txt1.setText(c);
viewImage.setImageBitmap(Global.img);
}
else if (requestCode==CAMERA_REQUEST_CODE)
{
}
}
Activity B
selectImage();
public void selectImage() {
final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(ImageFitScreen.this);
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
//pic = f;
startActivityForResult(intent, 1);
} else if (options[item].equals("Choose from Gallery"))
{
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
finish();
}
}
});
builder.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK){
dialog.dismiss();
finish();
}
return true;
}
});
builder.show();
}
@Override
public void onBackPressed() {
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
}
But when I click the cancel
in the dialog, the app crashed. I'm sure it crashed is because
c= data.getStringExtra("text");
because it not crashed when c
has removed. But I need "text" in Activity B return to A.
Ok
button in Activity B
ok.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
Intent returnIntent=new Intent();
text=t.getText().toString();
returnIntent.putExtra("text", text);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
LogCat error
11-04 13:17:19.321 18437-18437/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 18437
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.example.project.project/com.example.project.project.Project1}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3681)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3724)
at android.app.ActivityThread.access$1400(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.project.project.Project1.onActivityResult(Project1.java:98)
Upvotes: 0
Views: 314
Reputation: 436
First of all you are not actually setting the value of "text". But still when you cancel Intent is null therefore you can't do
data.getStringExtra("text")
You should first do a check:
if (data !=null && data.hasExtra("text")){
c= data.getStringExtra("text");
txt1.setText(c);
}
Then in your onbackpressed you want to set the "text" extra like so:
@Override
public void onBackPressed() {
Intent returnIntent = new Intent();
returnIntent.putExtra("text", YOUR_STRING_HERE);
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
}
and also maybe on your dialog cancel(depending on what you are trying to accomplish)
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
Intent returnIntent = new Intent();
returnIntent.putExtra("text", YOUR_STRING_HERE);
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
}
Upvotes: 1
Reputation: 3734
You are not sending any text
field through intent to Activity A
.
Set the text
in onBackPressed()
in Activity B
@Override
public void onBackPressed() {
Intent returnIntent = new Intent();
// Set the text here
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
}
like
@Override
public void onBackPressed() {
Intent returnIntent = new Intent();
returnIntent.putExtra("text", YOUR_TEXT);
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
}
Upvotes: 1