Reputation: 9065
My MainActivity:
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.hello.MESSAGE";
private EditText text1;
private EditText text2;
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1 = (EditText) findViewById(R.id.editText1);
text2 = (EditText) findViewById(R.id.editText2);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage(v);
}
});
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, MessageActivity.class);
String message = text1.getText().toString() + " " + text2.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
}
My MessageActivity:
public class MessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = (TextView) findViewById(R.id.textView3);
textView.setText("Hello" + message);
}
}
The button is declared in the layout file this way:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="47dp"
android:text="@string/main_button_1"
android:onClick="sendMessage" />
Anyone can see what's wrong here? The app runs without problem in the emulator, but when I click in the button, nothing happens.
Upvotes: 0
Views: 80
Reputation: 645
I think you forgot to call startActivity(intent)
on sendMessage(View view)
method.
Ps: To listen the click of the button you need only this:
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage(v);
}
});
or this (in xml), not both.
android:onClick="sendMessage" />
Upvotes: 2