onActivityResult中有多个请求代码

时间:2012-11-20 09:07:27

标签: java android android-intent switch-statement onactivityresult

我有两个独立的startActivityForResult,它们使用不同的活动。我希望能够在我的主要活动中处理这些活动的结果。

然而,当返回主活动时,执行该函数(例如,在requestCodeText的情况下,btn3上的文本被设置为正确的值)但是应用程序冻结。

这是我的代码:

    final int request_CodeNumber = 1;
final int request_CodeText = 2;

    public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    switch(requestCode) {
    case request_CodeText:
        Uri uri = data.getData();
        final Button btn3 = (Button)findViewById(R.id.btnMessage);
        if(resultCode == RESULT_OK) {
            btn3.setText((String) uri.toString());
        }
        break;

    case request_CodeNumber:
        Uri uri2 = data.getData();
        Cursor c = null;
        try {
            c = getContentResolver().query(uri2, new String[]{
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    ContactsContract.CommonDataKinds.Phone.TYPE,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
                    null, null, null);
            if (c!= null && c.moveToFirst()) {
                String number = c.getString(0);
                int type = c.getInt(1);
                String contactName = c.getString(2);
                showSelectedNumber(type, number, contactName);
            }
        } finally {
            if (c != null) {
                c.close();
            }
        }
    break;
    }
}

我在logcat中得到以下内容:

11-20 21:52:45.155: D/dalvikvm(655): GC_FOR_ALLOC freed 66K, 4% free 8004K/8259K, paused 85ms, total 97ms
11-20 21:52:45.215: I/dalvikvm-heap(655): Grow heap (frag case) to 10.633MB for 2903056-byte allocation
11-20 21:52:45.485: D/dalvikvm(655): GC_CONCURRENT freed <1K, 3% free 10838K/11143K, paused 122ms+20ms, total 273ms
11-20 21:52:47.055: D/libEGL(655): loaded /system/lib/egl/libEGL_emulation.so
11-20 21:52:47.076: D/(655): HostConnection::get() New Host Connection established 0x2a0d7d68, tid 655
11-20 21:52:47.218: D/libEGL(655): loaded /system/lib/egl/libGLESv1_CM_emulation.so
11-20 21:52:47.236: D/libEGL(655): loaded /system/lib/egl/libGLESv2_emulation.so
11-20 21:52:47.425: W/EGL_emulation(655): eglSurfaceAttrib not implemented
11-20 21:52:47.476: D/OpenGLRenderer(655): Enabling debug mode 0
11-20 21:52:54.076: W/EGL_emulation(655): eglSurfaceAttrib not implemented
11-20 21:52:59.646: W/EGL_emulation(655): eglSurfaceAttrib not implemented

0 个答案:

没有答案