Hi. I have followed the instructions at the following link to display a message on a button
click in my first Android app but when I click the button, nothing happens.
Have I done something wrong? Thanks
http://developer.android.com/training/basics/firstapp/starting-activity.html
package co.webexcel.myfirstapp;
import android.annotation.Suppres
sLint;
import android.annotation.TargetA
pi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Nav
Utils;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
//Additionally, you must add the @SuppressLint("NewApi") tag to the
//onCreate() method to avoid lint errors.
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstan
ceState);
setContentView(R.layout.ac
tivity_dis
play_messa
ge);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(Main
Activity.E
XTRA_MESSA
GE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
//setupActionBar();
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_C
ODES.HONEY
COMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYC
OMB) {
getActionBar().setDisplayH
omeAsUpEna
bled(true)
;
}
}
@Override
//this handles the behavior for the action bar's Up behavior
public boolean onOptionsItemSelected(Menu
Item item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
//
http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSam
eTask(this
);
return true;
}
return super.onOptionsItemSelecte
d(item);
}
}
setContentView(R.layout.ac
You will want to set the content view after your have set the text view.