简体   繁体   中英

Sending a string from one activity to another

EDITED in order to keep you up to date.

Thanks for the answers. Tried them all, but that didn't solve the problem. The string and name were correct. (chosenone being the string that I want to send and the other the name).

I've spent a few hours today playing with the code and still it crashes... (solved a few of the errors).

This is from activity 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();
        }
    }
};

and activity 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

If I remove the getextras() and getstring() lines from activity 1 it doesn't crash. Could the problem be there? It seems to me like it doesn't send anything to the main activity...

Thanks again,

Your string is in the Intent data

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

You are not getting the right Intent in your activity1 . Change the line that says

Bundle fromtheother = getIntent().getExtras();

to

Bundle fromtheother = data.getExtras();

data is the Intent that activity2 will return.

You're retrieving data the wrong way.

  intent.putExtra(EXTRA_DEVICE_ADDRESS, chosenone);

Format:

intent.putExtra("StringReference",data);

Here, EXTRA_DEVICE_ADDRESS if actually String is the reference by which you should retrieve the data.

So technically, your code in the first activity will be:

String EXTRA_DEVICE_ADDRESS = intent1.getExtras().getString("EXTRA_DEVICE_ADDRESS");

Now, the correction.

intent.putExtra("chosenone", EXTRA_DEVICE_ADDRESS);

First activity:

String EXTRA_DEVICE_ADDRESS = intent1.getExtras().getString("chosenone");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM