Reputation: 11141
I am working on a VCard application. here I have started Default Contacts activity(Address Book).
Now, the flow of application is like this,
Now, when the user presses Back Button
without selecting any contact, the app gets force closed.
I have overidden BackKeyPress()
event, but its not working. The app is still getting force closed.
I am posting the code for reference,
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
Log.d("In Oncreate", "Activity Result");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
Toast.makeText(getApplicationContext(), "Back Pressed", 5000).show();
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (1) :
counter = "Yes";
Log.d("My Tag", data.getExtras().keySet().toString());
name_selected = data.getExtras().getString("android.intent.extra.shortcut.NAME");
Log.d("My Tag", name_selected));
Intent i = new Intent(getApplicationContext(), BusinessCardActivity.class);
i.putExtra("name", name_selected);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(i);
}
}
}
Logcat Errors:-
12-17 18:41:09.399: E/AndroidRuntime(23456): FATAL EXCEPTION: main
12-17 18:41:09.399: E/AndroidRuntime(23456): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.android.business/com.android.business.ReadContacts}: java.lang.NullPointerException
12-17 18:41:09.399: E/AndroidRuntime(23456): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
12-17 18:41:09.399: E/AndroidRuntime(23456): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
12-17 18:41:09.399: E/AndroidRuntime(23456): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
12-17 18:41:09.399: E/AndroidRuntime(23456): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
12-17 18:41:09.399: E/AndroidRuntime(23456): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 18:41:09.399: E/AndroidRuntime(23456): at android.os.Looper.loop(Looper.java:123)
12-17 18:41:09.399: E/AndroidRuntime(23456): at android.app.ActivityThread.main(ActivityThread.java:3729)
12-17 18:41:09.399: E/AndroidRuntime(23456): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 18:41:09.399: E/AndroidRuntime(23456): at java.lang.reflect.Method.invoke(Method.java:507)
12-17 18:41:09.399: E/AndroidRuntime(23456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
12-17 18:41:09.399: E/AndroidRuntime(23456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
12-17 18:41:09.399: E/AndroidRuntime(23456): at dalvik.system.NativeStart.main(Native Method)
12-17 18:41:09.399: E/AndroidRuntime(23456): Caused by: java.lang.NullPointerException
12-17 18:41:09.399: E/AndroidRuntime(23456): at com.android.business.ReadContacts.onActivityResult(ReadContacts.java:133)
12-17 18:41:09.399: E/AndroidRuntime(23456): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
12-17 18:41:09.399: E/AndroidRuntime(23456): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
Here the user is supposed to select a contact from the list, but if the user presses back button without selecting any contact, the app gets force closed.
Upvotes: 2
Views: 823
Reputation: 2528
In your OnActivityResult() do this..
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (1) :
if (data != null && resultCode == RESULT_OK) {
counter = "Yes";
// Do some operations on the selected contact
}
}
}
Upvotes: 2
Reputation: 6738
use this...
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case 1:
if(data!=null&&resultCode==RESULT_OK){
counter = "Yes";
Log.d("My Tag", data.getExtras().keySet().toString());
name_selected = data.getExtras().getString("android.intent.extra.shortcut.NAME");
Log.d("My Tag", name_selected));
Intent i = new Intent(getApplicationContext(), BusinessCardActivity.class);
i.putExtra("name", name_selected);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(i);
}
}
}
Upvotes: 3
Reputation: 109237
I have overidden BackKeyPress() event, but its not working. The app is still getting force closed.
Because the backKeyPressed()
event responds to Default Contacts activity(Address Book)
. Not to your Android Application's Activity.
As I seen your code for onActivityResult()
and Without any logcat errors I think this crash may be happen at Default Contacts activity(Address Book)
side not at your application side. Also You have to check for is Default Contacts activity(Address Book)
is handled any Intent
(Resulted Intent) for onActivityResult()
as respond purpose to other application.
So logcat errors will be more helpful over here.
EDIT:
Yeah As I Have a doubt, Your resulted data is NULL
before doing operation on Intent
just check whether data
is null or not,
if(data != null)
{
Log.d("My Tag", data.getExtras().keySet().toString());
name_selected = data.getExtras().getString("android.intent.extra.shortcut.NAME");
Log.d("My Tag", name_selected));
Intent i = new Intent(getApplicationContext(), BusinessCardActivity.class);
i.putExtra("name", name_selected);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(i);
}
Upvotes: 1
Reputation: 708
Your application gets force closed due to a NullPointerException thrown in line 133 in your onActivityResult method. Check if the result is valid and put some try catch in there.
12-17 18:41:09.399: E/AndroidRuntime(23456): Caused by: java.lang.NullPointerException
12-17 18:41:09.399: E/AndroidRuntime(23456): at com.android.business.ReadContacts.onActivityResult(ReadContacts.java:133)
Upvotes: 1
Reputation: 1037
Are you managing the case that there's no selected contact? If the user press back and no contact is selected, in your onActivityResult you should handle that the "contact" is null.
Upvotes: -1