2

onActivityResult の使用に問題があり、以下のコードがクラッシュする理由がわかりません。

メインアクティビティに、結果のために別のアクティビティを開始するボタンがあります。

    //---Setting the button for choosing a message---
    btn3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            // startActivity(new Intent("kurt.steveboss.textChooserView")); 
            startActivityForResult(new Intent(
                    "kurt.steveboss.textChooserView"),
                    request_CodeText);
        }

    });

これにより、新しいビューが表示されます。このビューでは、ユーザーがボタンをクリックすると、EditText 内のテキストがキャプチャされ、結果として mainActivity に返されます。

        //---Setting the button for generating a random message---
    btn6.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {

            Intent data = new Intent();

            //Extract the message from the EditText 
            data.setData(Uri.parse(txtMessage.getText().toString()));
            setResult(RESULT_OK, data);

            //---closes the activity---
            finish();               
        }
    });  

次に、mainActivity に onReceive があります (ここには複数のインテントがあり、これはもちろん「request_CodeText」のケースをリッスンしています)

    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());
            }
            else if(resultCode == RESULT_CANCELED) {
                //no action
            }
        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;
    }
}

この onActivityResult は、ボタン 3 のテキストが更新される範囲で機能します (したがって、その時点まで機能しています)。しかし、LogCat で次のようにアプリがクラッシュします。

12-21 15:10:15.995: I/ActivityManager(2098): START {intent.toShortString} from pid 19907
12-21 15:10:16.000: I/power(2098): *** acquire_dvfs_lock : lockType : 1  freq : 1000000 
12-21 15:10:16.000: D/PowerManagerService(2098): acquireDVFSLockLocked : type : DVFS_MIN_LIMIT  frequency : 1000000  uid : 1000  pid : 2098  tag : ActivityManager
12-21 15:10:16.000: W/ActivityManager(2098): mDVFSLock.acquire()
12-21 15:10:16.010: W/InputDispatcher(2098): channel ~ Consumer closed input channel or an error occurred.  events=0x8
12-21 15:10:16.010: D/InputDispatcher(2098): [drainOutboundQueueLocked] initialize cntPair(sender-receiver)
12-21 15:10:16.010: E/InputDispatcher(2098): channel ~ Channel is unrecoverably broken and will be disposed!
12-21 15:10:16.085: D/dalvikvm(2098): GC_FOR_ALLOC freed 821K, 47% free 22959K/43079K, paused 82ms
12-21 15:10:16.090: I/dalvikvm-heap(2098): Grow heap (frag case) to 25.390MB for 2903056-byte allocation
12-21 15:10:16.175: D/dalvikvm(2098): GC_FOR_ALLOC freed 15K, 41% free 25779K/43079K, paused 65ms
12-21 15:10:16.190: W/InputDispatcher(2098): Attempted to unregister already unregistered input channel
12-21 15:10:16.190: I/SurfaceFlinger(1904): id=2501 Removed MainActivity idx=2 Map Size=4
12-21 15:10:16.190: I/SurfaceFlinger(1904): id=2501 Removed MainActivity idx=-2 Map Size=4
12-21 15:10:16.190: D/KeyguardViewMediator(2098): setHidden false
12-21 15:10:16.190: W/SignalStrength(2098): getGsmLevel=2
12-21 15:10:16.190: W/SignalStrength(2226): getGsmLevel=2
12-21 15:10:16.190: W/SignalStrength(2226): getLevel=2 (SignalStrength: 99 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 gsm|lte 2)
12-21 15:10:16.190: W/SignalStrength(2226): getGsmLevel=2
12-21 15:10:16.190: W/SignalStrength(2226): getLevel=2 (SignalStrength: 99 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 gsm|lte 2)
12-21 15:10:16.190: W/InputDispatcher(2098): channel ~ Consumer closed input channel or an error occurred.  events=0x8
12-21 15:10:16.190: E/InputDispatcher(2098): channel ~ Channel is unrecoverably broken and will be disposed!
12-21 15:10:16.190: W/InputDispatcher(2098): Attempted to unregister already unregistered input channel
12-21 15:10:16.190: I/WindowManager(2098): WINDOW DIED Window{420e4858 XXXX.XXXXXXXXX.XXXXXXXXXXXXXXXX/XXXX.XXXXXXXXX.XXXXXXXXXXXXXXXX.XXXXXXXXXXXX paused=false}
12-21 15:10:16.190: D/STATUSBAR-NetworkController(2226): onSignalStrengthsChanged signalStrength=SignalStrength: 99 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 gsm|lte 2 level=2
12-21 15:10:16.190: I/WindowManager(2098): WIN DEATH: Window{426389d0 XXXX.XXXXXXXXX.XXXXXXXXXXXXXXXX/XXXX.XXXXXXXXX.XXXXXXXXXXXXXXXX.XXXXXXXXXXXXXXX paused=false}
12-21 15:10:16.195: D/KeyguardViewMediator(2098): setHidden false
12-21 15:10:16.195: W/SignalStrength(2098): getLevel=2 (SignalStrength: 99 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 gsm|lte 2)
12-21 15:10:16.195: W/WindowManager(2098): Failed looking up window
12-21 15:10:16.195: W/WindowManager(2098): java.lang.IllegalArgumentException: Requested window android.os.BinderProxy@426977f0 does not exist
12-21 15:10:16.195: W/WindowManager(2098):  at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:7807)
4

1 に答える 1

3

これはあなたが他の活動を始めることになっている方法です。

Intent intent = new Intent(yourclassname.this, textChooserView.class);
startActivityForResult( intent, request_CodeText);

それ以外の

  startActivityForResult(new Intent(
            "kurt.steveboss.textChooserView"),
            request_CodeText);

また、ActivityResultで、とを確認する必要がresultCode == RESULT_OKありdata is different to nullます。これは、null例外からあなたを助けます。

于 2012-12-21T04:39:57.210 に答える