Avatar of Murray Brown
Murray Brown
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Android Problem displaying message

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.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
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(savedInstanceState);
            setContentView(R.layout.activity_display_message);
            // 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(MainActivity.EXTRA_MESSAGE);

          // 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_CODES.HONEYCOMB)
      private void setupActionBar() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                  getActionBar().setDisplayHomeAsUpEnabled(true);
            }
      }



      @Override
      //this handles the behavior for the action bar's Up behavior
      public boolean onOptionsItemSelected(MenuItem 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.navigateUpFromSameTask(this);
                  return true;
            }
            return super.onOptionsItemSelected(item);
      }

}
Android

Avatar of undefined
Last Comment
Murray Brown

8/22/2022 - Mon
Chris Harte

You are calling the setContentView() method twice. Comment out the first one

setContentView(R.layout.activity_display_message);

You will want to set the content view after your have set the text view.
Murray Brown

ASKER
Hi. Thanks, but still doesn't work
I get the following at the bottom of the screen
[2013-12-03 09:03:32 - MyFirstApp] Starting activity co.webexcel.myfirstapp.MainActivity on device 0009022458cf9f
[2013-12-03 09:03:33 - MyFirstApp] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=co.webexcel.myfirstapp/.MainActivity }
[2013-12-03 09:03:33 - MyFirstApp] ActivityManager: Warning: Activity not started, its current task has been brought to the front
Chris Harte

That error message means it cannot start the activity MainActivity. Have you spelt it correctly and it is in the manifest xml?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Murray Brown

ASKER
Hi. The activity started this time by rebooting my machine

[2013-12-03 16:43:10 - MyFirstApp] Success!
[2013-12-03 16:43:10 - MyFirstApp] Starting activity co.webexcel.myfirstapp.MainActivity on device 0009022458cf9f
[2013-12-03 16:43:11 - MyFirstApp] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=co.webexcel.myfirstapp/.MainActivity }
Chris Harte

So, is it working then?
Murray Brown

ASKER
No, unfortunately not. Is it perhaps the type of phone I have? Is there any way I can debug on my computer or see what is happening there?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Chris Harte

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Murray Brown

ASKER
Thanks very much MunterMan. I appreciate the advice! I had already found other bugs in the few pages on that site that I had gone through