How can I use a string of an activity, in another activity

1

I have a String value in my class encuesta.java , and I want to use that String in class res8.java , both are Activity .

The two classes are in the same package.

Manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pablo.myapplication">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Resultados" />
    <activity android:name=".encuesta" />
    <activity android:name=".resProfesores" />
    <activity android:name=".res11" />
    <activity android:name=".res9" />
    <activity android:name=".res8"></activity>
</application>

</manifest>

On the Android monitor:

03-06 14:51:24.403 7172-7172/com.example.pablo.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pablo.myapplication/com.example.pablo.myapplication.res8}: java.lang.NullPointerException
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
                                                                               at android.app.ActivityThread.access$700(ActivityThread.java:134)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                               at android.os.Looper.loop(Looper.java:137)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:4867)
                                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:511)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
                                                                               at dalvik.system.NativeStart.main(Native Method)
                                                                            Caused by: java.lang.NullPointerException
                                                                               at com.example.pablo.myapplication.res8.onCreate(res8.java:20)
                                                                               at android.app.Activity.performCreate(Activity.java:5047)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117) 
                                                                               at android.app.ActivityThread.access$700(ActivityThread.java:134) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                               at android.os.Looper.loop(Looper.java:137) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:4867) 
                                                                               at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                               at java.lang.reflect.Method.invoke(Method.java:511)
    
asked by Pablo Gonzalez 06.03.2017 в 17:00
source

3 answers

1

It would be send send a String data between Activities.

The sending of data between activities is generally done by a Bundle in which values are added and that bundle is sent through a Intent to Activity destination.

    Intent intent = new Intent(encuesta.this, res8.class);
    intent.putExtra("dato", "StackOverflow!");
    startActivity(intent);      

Values are obtained in% destination% co through getExtras ()

String datorecibido = getIntent().getExtras().getString("dato");

or simply:

String datorecibido = getIntent().getStringExtra("dato");

In the Activity that receives the value, you can use the value and assign it to your Activity :

String datorecibido = getIntent().getExtras().getString("dato");
TextView myTextView = (TextView)findViewById(R.id.reslt); 
myTextView.setText(datorecibido); 
    
answered by 06.03.2017 в 17:39
0

Example:

This line in encuesta.java

String text = textView.getText().toString();

Now you need to send this text to the other Activity where you want to use it.
This line in encuesta.java

startActivity(res8.buildIntent(this, text));

When you start the Activity you get the text of the extra you sent.
This code in res8.java

public static String EXTRA_TEXT = "extra_text";

public static Intent buildIntent(Context context, String text){
    Intent intent = new Intent(context, MyActivity.class);
    intent.putExtra(EXTRA_TEXT, text);
    return intent;
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_activity);

    Bundle bundle = getIntent().getExtras();
    if(bundle != null){
        String text = bundle.getString(EXTRA_TEXT);
    }
}

From: link

In your case I understand that you take the text in encuesta.java and at some point you want to change the Activity res8.java and keep the text in the new Activity.

You should add the extra and the method buildIntent() to res8.java and use it from encuesta.java within startActivity()

    
answered by 06.03.2017 в 17:41
0

If your string is something that you define, add it as a resource in the strings.xml file

<string name="cadena">Mi cadena</string>

If it is something that the user enters, send it as a parameter when creating the next activity

Intent intent = new Intent(encuesta.this, res8.class);
intent.putExtra("mi_cadena", miString);
startActivity(intent); 

and you get it back in the next activity on the onCreate

if(getIntent().getExtras() != null && getIntent().getExtras().hasExtra("mi_cadena")) {
    String miString = getIntent().getExtras().getString("mi_cadena");
}

the if is to verify if the value exists, if you do not have it already you could put a sentence that does something in case of not sending the data

    
answered by 06.03.2017 в 18:44