我正在开发一个 VCard 应用程序。在这里,我已经开始了默认联系人活动(通讯簿)。
现在,申请流程是这样的,
- 用户启动应用程序。
- 默认通讯录打开。
- 用户从列表中选择一个联系人,然后创建所选联系人的 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)
在这里,用户应该从列表中选择一个联系人,但如果用户在没有选择任何联系人的情况下按下返回按钮,应用程序将被强制关闭。