0

编辑以使您了解最新情况。

感谢您的回答。都试过了,但这并没有解决问题。字符串和名称是正确的。(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() 行,它不会崩溃。问题可能存在吗?在我看来,它没有向主要活动发送任何内容......

再次感谢,

4

3 回答 3

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(); 
}
于 2013-05-27T20:26:24.473 回答
0

你没有得到正确Intentactivity1. 更改显示的行

Bundle fromtheother = getIntent().getExtras();

Bundle fromtheother = data.getExtras();

dataIntent那个activity2会回来的。

于 2013-05-28T01:13:22.893 回答
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");
于 2013-05-28T01:14:45.947 回答