2

我正在开发一个 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)

在这里,用户应该从列表中选择一个联系人,但如果用户在没有选择任何联系人的情况下按下返回按钮,应用程序将被强制关闭。

4

5 回答 5

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);
   }
  }
}
于 2012-12-17T13:41:03.737 回答
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
     }
   }
}
于 2012-12-17T13:16:25.547 回答
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

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);
}
于 2012-12-17T13:10:30.650 回答
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)
于 2012-12-17T13:17:31.227 回答
-1

您是否正在处理没有选定联系人的情况?如果用户按下并没有选择联系人,则在您的 onActivityResult 中,您应该处理“联系人”为空。

于 2012-12-17T13:11:36.587 回答