例外:交付结果失败

时间:2012-10-21 07:51:30

标签: android android-intent runtime

我收到以下异常:

10-06 15:54:24.558: E/AndroidRuntime(2858): FATAL EXCEPTION: main
10-06 15:54:24.558: E/AndroidRuntime(2858): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://contacts/people/1 flg=0x1 }} to activity {com.gaurav.androidpractice/com.gaurav.androidpractice.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
10-06 15:54:24.558: E/AndroidRuntime(2858):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
10-06 15:54:24.558: E/AndroidRuntime(2858):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
10-06 15:54:24.558: E/AndroidRuntime(2858):     at android.app.ActivityThread.access$1100(ActivityThread.java:130)
10-06 15:54:24.558: E/AndroidRuntime(2858):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
10-06 15:54:24.558: E/AndroidRuntime(2858):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 15:54:24.558: E/AndroidRuntime(2858):     at android.os.Looper.loop(Looper.java:137)
10-06 15:54:24.558: E/AndroidRuntime(2858):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-06 15:54:24.558: E/AndroidRuntime(2858):     at java.lang.reflect.Method.invokeNative(Native Method)
10-06 15:54:24.558: E/AndroidRuntime(2858):     at 
java.lang.reflect.Method.invoke(Method.java:511)
10-06 15:54:24.558: E/AndroidRuntime(2858):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-06 15:54:24.558: E/AndroidRuntime(2858):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-06 15:54:24.558: E/AndroidRuntime(2858):     at dalvik.system.NativeStart.main(Native Method)
10-06 15:54:24.558: E/AndroidRuntime(2858): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0

在以下代码中:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e(TAG, getMethodName());

        if(resultCode == Activity.RESULT_OK){
            Cursor cursor = getContentResolver().query(data.getData(), new String[]       {Contacts.DISPLAY_NAME}, null, null, null);
            if(cursor.moveToFirst()){
                int columnindex = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
                String name = getString(columnindex);
                nameTextView.setText(name);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    nameTextView = (TextView)this.findViewById(R.id.name);
    Uri uri = Uri.parse("content://contacts/people");
    Intent intent = new Intent(Intent.ACTION_PICK, uri);
    startActivityForResult(intent, PICK_CONTACT_SUBACTIVITY);
}

请说明问题的原因和原因,以及如何解决这个问题。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

String name = getString(columnindex)中,您正在调用Context.getString,这在这里完全没用(它加载字符串资源并期望资源ID)。请改用cursor.getString来获取游标列的内容。