我在使用 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)