已编辑,以便让您了解最新动态。
感谢您的回答。尝试了所有,但这并没有解决问题。 字符串和名称是正确的。 (selectedone是我要发送的字符串,另一个是名字)。
我今天花了几个小时玩代码然后它仍然崩溃了......(解决了一些错误)。
这来自活动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();
}
}
}
logcat的:
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()行,它就不会崩溃。问题可能存在吗? 在我看来它似乎没有向主要活动发送任何东西......
再次感谢,
答案 0 :(得分:0)
您的字符串位于Intent数据
中public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/*Make sure we got the right device */
String EXTRA_DEVICE_ADDRESS = data.getStringExtra("theotherdude");
// Get the device MAC address, which is the last 17 chars in the View
String address = EXTRA_DEVICE_ADDRESS.substring(EXTRA_DEVICE_ADDRESS.length() - 17);
String name = EXTRA_DEVICE_ADDRESS.substring(0, EXTRA_DEVICE_ADDRESS.length() - 17);
otherDeviceName.setText(R.string.with + name);
Toast.makeText(this, address, Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
您Intent
未获得正确的activity1
。更改显示
Bundle fromtheother = getIntent().getExtras();
到
Bundle fromtheother = data.getExtras();
data
是Intent
将返回的activity2
。
答案 2 :(得分:0)
您正在以错误的方式检索数据。
intent.putExtra(EXTRA_DEVICE_ADDRESS, chosenone);
格式:
intent.putExtra("StringReference",data);
此处EXTRA_DEVICE_ADDRESS
如果实际String
是您应该通过其检索数据的参考。
从技术上讲,第一项活动中的代码将是:
String EXTRA_DEVICE_ADDRESS = intent1.getExtras().getString("EXTRA_DEVICE_ADDRESS");
现在,纠正。
intent.putExtra("chosenone", EXTRA_DEVICE_ADDRESS);
第一项活动:
String EXTRA_DEVICE_ADDRESS = intent1.getExtras().getString("chosenone");