gpt4 book ai didi

java - onActivityResult 崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 09:02:37 26 4
gpt4 key购买 nike

我在使用 onActivityResult 时遇到问题,无法弄清楚为什么下面的代码会崩溃。

我的主要 Activity 中有一个按钮,可以启动另一个 Activity 以获取结果。

    //---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);
}

});

这带来了一个新的观点。在此 View 中,当用户单击按钮时,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(这里有不止一个 Intent ,这个当然是监听“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)

最佳答案

这就是您应该如何开始其他 Activity 。

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

代替

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

并且在 ActivityResult 上,您还应该检查 resultCode == RESULT_OKdata 是否不同于 null。这将帮助您摆脱 null 异常。

关于java - onActivityResult 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13984263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com