我正在开发一个VCard应用程序。这里我已经开始默认联系人活动(地址簿)。
现在,应用程序流程就是这样,
现在,当用户在没有选择任何联系人的情况下按下Back Button
时,应用程序将被强制关闭。
我已经覆盖BackKeyPress()
事件,但它不起作用。该应用程序仍在关闭。
我发布的代码供参考,
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错误: -
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)
此处用户应该从列表中选择一个联系人,但如果用户在没有选择任何联系人的情况下按下后退按钮,该应用程序将被强制关闭。
答案 0 :(得分:3)
使用此...
@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);
}
}
}
答案 1 :(得分:2)
在你的OnActivityResult()中这样做..
@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
}
}
}
答案 2 :(得分:1)
I have overidden BackKeyPress() event, but its not working. The app is still getting force closed.
因为backKeyPressed()
事件会响应Default Contacts activity(Address Book)
。不是你的Android应用程序的活动。
当我看到你的代码onActivityResult()
和没有任何logcat错误时,我认为这种崩溃可能发生在Default Contacts activity(Address Book)
方,而非你的应用方。此外,您必须检查Default Contacts activity(Address Book)
是否为Intent
处理任何onActivityResult()
(结果意图)作为对其他应用程序的响应。
所以 logcat错误会更有帮助。
修改强>
是的我怀疑,您的数据是NULL
在对Intent
进行操作之前,只需检查data
是否 null ,
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);
}
答案 3 :(得分:1)
由于onActivityResult方法第133行引发了NullPointerException,您的应用程序将被强制关闭。检查结果是否有效并在那里放一些try catch。
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)
答案 4 :(得分:-1)
您是否在管理没有选定联系人的情况?如果用户按下并且未选择任何联系人,则在onActivityResult中,您应该处理“联系人”为空。