Question

I readed a lot about putExtra but my application still doesn´t works(applicaton shutdown) I just want to send my string value in my editText from the MainActivity to the AnotherActivity textView. Here is my MainActivity.java.

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText et = (EditText)findViewById(R.id.tv);

    Button bn = (Button)findViewById(R.id.button1);
    bn.setOnClickListener(new OnClickListener() {

              public void onClick(View arg0) {
                    Intent intent = new Intent(getApplicationContext(), AnotherActivity.class);
                    intent.putExtra("text", et.getText().toString());
                    startActivity(intent);
              }
        });
}}

Here is my activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/et"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/button1"
    android:ems="10"
    android:inputType="text"
    android:text="@string/hello_world" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/et"
    android:layout_marginLeft="18dp"
    android:text="@string/hello_world" />

Here is my AnotherActivity.java

enter code here

public class AnotherActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_another);

    String name = getIntent().getStringExtra("text");

    TextView tv = (TextView)findViewById(R.id.tv);
    tv.setText(name);
}}    

And here is my activity_another.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AnotherActivity" >

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="107dp"
    android:layout_marginTop="190dp"
    android:text="@string/title_activity_another" />

Thank you for answer. I am newbie :)

Was it helpful?

Solution

Don't worry, you have correctly understood how to use Intent extras.

The problem is, you are trying to find an EditText (with id R.id.tv) that is in your second Activity's layout in your first Activity's layout, and that leads to a NullPointerException in your OnClickListener.

Change this line of your first Activity:

final EditText et = (EditText)findViewById(R.id.tv);

to:

final EditText et = (EditText)findViewById(R.id.et);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top