编辑以使您了解最新情况。
感谢您的回答。都试过了,但这并没有解决问题。字符串和名称是正确的。(chosenone 是我要发送的字符串,另一个是名称)。
我今天花了几个小时玩代码,但它仍然崩溃......(解决了一些错误)。
这是来自活动 2:
// The on-click listener for all devices in the ListViews
private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
// Cancel discovery because it's costly and we're about to connect
mBluetoothAdapter.cancelDiscovery();
/*Send string to the other activity */
String chosenone = ((TextView) v).getText().toString();
Intent intent = new Intent();
intent.putExtra("theotherdude", chosenone);
toasttest(chosenone);
/*To avoid it from going back with nothing*/
if(chosenone != null){
// Set result and finish this Activity
setResult(Activity.RESULT_OK, intent);
finish();
}
}
};
和活动1:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check which request we're responding to
if (requestCode == MY_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
//String theotherdude = fromtheother.getString("theotherdude");
Intent intent = getIntent();
String id = intent.getStringExtra("theotherdude");
// Get the device MAC address, which is the last 17 chars in the View
String address = id.substring(id.length() - 17);
String name = id.substring(0, id.length() - 17);
otherDeviceName.setText(R.string.with + name);
Toast.makeText(this, address, Toast.LENGTH_SHORT).show();
}
}
}
日志猫:
05-28 19:46:24.625: I/Process(19972): Sending signal. PID: 19972 SIG: 9
05-28 19:48:55.086: D/libEGL(20267): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
05-28 19:48:55.094: D/libEGL(20267): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
05-28 19:48:55.094: D/libEGL(20267): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
05-28 19:48:55.188: D/OpenGLRenderer(20267): Enabling debug mode 0
05-28 19:48:58.281: D/OpenGLRenderer(20267): Flushing caches (mode 0)
05-28 19:49:00.688: D/AndroidRuntime(20267): Shutting down VM
05-28 19:49:00.688: W/dalvikvm(20267): threadid=1: thread exiting with uncaught exception (group=0x40bec1f8)
05-28 19:49:00.688: E/AndroidRuntime(20267): FATAL EXCEPTION: main
05-28 19:49:00.688: E/AndroidRuntime(20267): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { (has extras) }} to activity {com.andrecl.interapption/com.andrecl.interapption.MainActivity}: java.lang.NullPointerException
05-28 19:49:00.688: E/AndroidRuntime(20267): at android.app.ActivityThread.deliverResults(ActivityThread.java:2994)
05-28 19:49:00.688: E/AndroidRuntime(20267): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3037)
05-28 19:49:00.688: E/AndroidRuntime(20267): at android.app.ActivityThread.access$1100(ActivityThread.java:128)
05-28 19:49:00.688: E/AndroidRuntime(20267): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
05-28 19:49:00.688: E/AndroidRuntime(20267): at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 19:49:00.688: E/AndroidRuntime(20267): at android.os.Looper.loop(Looper.java:137)
05-28 19:49:00.688: E/AndroidRuntime(20267): at android.app.ActivityThread.main(ActivityThread.java:4514)
05-28 19:49:00.688: E/AndroidRuntime(20267): at java.lang.reflect.Method.invokeNative(Native Method)
05-28 19:49:00.688: E/AndroidRuntime(20267): at java.lang.reflect.Method.invoke(Method.java:511)
05-28 19:49:00.688: E/AndroidRuntime(20267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-28 19:49:00.688: E/AndroidRuntime(20267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-28 19:49:00.688: E/AndroidRuntime(20267): at dalvik.system.NativeStart.main(Native Method)
05-28 19:49:00.688: E/AndroidRuntime(20267): Caused by: java.lang.NullPointerException
05-28 19:49:00.688: E/AndroidRuntime(20267): at com.andrecl.interapption.MainActivity.onActivityResult(MainActivity.java:113)
05-28 19:49:00.688: E/AndroidRuntime(20267): at android.app.Activity.dispatchActivityResult(Activity.java:4649)
05-28 19:49:00.688: E/AndroidRuntime(20267): at android.app.ActivityThread.deliverResults(ActivityThread.java:2990)
05-28 19:49:00.688: E/AndroidRuntime(20267): ... 11 more
05-28 19:49:11.719: I/Process(20267): Sending signal. PID: 20267 SIG: 9
如果我从活动 1 中删除 getextras() 和 getstring() 行,它不会崩溃。问题可能存在吗?在我看来,它没有向主要活动发送任何内容......
再次感谢,