mexcool
mexcool

Reputation: 11

Passing various editText to another activity

First of all I want to say that this is my first post so it may no be very well done, in general.

My issue is the following: I would like to use three different strings from three editTexts and display them in three TextViews in another activity. I have already been searching for different kinds of ways of doing it (arrays, bundle) but it continues crashing. Help me, please. Here you have my code:

Main activity

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    // Do something in response to button
    EditText eText1 = (EditText) findViewById(R.id.editText1);
    EditText eText2 = (EditText) findViewById(R.id.editText2);
    EditText eText3 = (EditText) findViewById(R.id.editText3);

    String m1 = eText1.getText().toString();
    String m2 = eText2.getText().toString();
    String m3 = eText3.getText().toString();

    Intent intent = new Intent(this, DisplayMessageActivity.class);
    intent.putExtra("m1",m1);
    intent.putExtra("m2",m2);
    intent.putExtra("m3",m3);
    startActivity(intent);
}

subActivity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

    // Get the message from the intent
    Bundle extras = getIntent().getExtras();


    // get the extras
        String a = extras.getString("m1");
    String b = extras.getString("m2");
    String c = extras.getString("m3");

    // Set the text views
    TextView tv1 = (TextView) findViewById(R.id.textView1);
    tv1.setText(a); // This is line 23
    TextView tv2 = (TextView) findViewById(R.id.textView2);
    tv2.setText(b);
    TextView tv3 = (TextView) findViewById(R.id.textView3);
    tv3.setText(c);
}

Edit1: I have changed what @Squonk said but still crashes. I don't know how to upload the logcat because it is too large for a text but I still can't upload images. Don't know what to do :(

Edit2: Thanks to @Squonk again. I have finally managed to add the logcat. This is the logcat for the code I recently changed in "Edit1".

03-29 16:05:39.905: E/AndroidRuntime(327): FATAL EXCEPTION: main
03-29 16:05:39.905: E/AndroidRuntime(327): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException
03-29 16:05:39.905: E/AndroidRuntime(327):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-29 16:05:39.905: E/AndroidRuntime(327):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-29 16:05:39.905: E/AndroidRuntime(327):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-29 16:05:39.905: E/AndroidRuntime(327):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-29 16:05:39.905: E/AndroidRuntime(327):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 16:05:39.905: E/AndroidRuntime(327):  at android.os.Looper.loop(Looper.java:123)
03-29 16:05:39.905: E/AndroidRuntime(327):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-29 16:05:39.905: E/AndroidRuntime(327):  at java.lang.reflect.Method.invokeNative(Native Method)
03-29 16:05:39.905: E/AndroidRuntime(327):  at java.lang.reflect.Method.invoke(Method.java:521)
03-29 16:05:39.905: E/AndroidRuntime(327):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-29 16:05:39.905: E/AndroidRuntime(327):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-29 16:05:39.905: E/AndroidRuntime(327):  at dalvik.system.NativeStart.main(Native Method)
03-29 16:05:39.905: E/AndroidRuntime(327): Caused by: java.lang.NullPointerException
03-29 16:05:39.905: E/AndroidRuntime(327):  at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:23)
03-29 16:05:39.905: E/AndroidRuntime(327):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-29 16:05:39.905: E/AndroidRuntime(327):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-29 16:05:39.905: E/AndroidRuntime(327):  ... 11 more

This is one of the textviews in fragment_display_message.xml. There are three of them. Could it be that when finding the textviews in the subactivity I have to write where to find them not to confuse with the main activity?

 <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="33dp"
    android:layout_marginTop="43dp"
    android:hint="@string/edit_message"
    android:textAppearance="?android:attr/textAppearanceLarge" />

Edit3: Eureka! I have finally discovered where the problem was. I had inserted the textviews in the fragment_display_message.xml instead of activity_display_message.xml. As in the mainactivity.java I use the fragment_main.xml I thought that in a subactivity would be the same. Well, I was wrong. Thanks to all! :D

Thank you for your support

Upvotes: 1

Views: 140

Answers (2)

artist
artist

Reputation: 6371

In the first activity,

Intent i = new Intent(MainActivity.this, SecondActivity.class); 
        i.putExtra("SomeValue", data);
        startActivity(i);

// In second activity

String str2 = getIntent().getExtras().getString("SomeValue");
    tvRecieve.setText(str2);

Upvotes: 0

Squonk
Squonk

Reputation: 48871

The problem is these two lines...

Bundle extras = new Bundle();
...
intent.putExtras(extras);

An Intent already carries a Bundle but what you're doing is creating a second Bundle and adding that as an 'extra'.

Remove both of those lines from your main Activity and change the following lines...

extras.putString("m1", m1);
extras.putString("m2", m2);
extras.putString("m3", m3);

...to...

intent.putExtra("m1", m1);
intent.putExtra("m2", m2);
intent.putExtra("m3", m3);

Upvotes: 1

Related Questions