Solved

Android Problem displaying message

Posted on 2013-12-02
8
735 Views
Last Modified: 2013-12-03
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);
      }

}
0
Comment
Question by:murbro
  • 4
  • 4
8 Comments
 
LVL 16

Expert Comment

by:Chris Harte
ID: 39690712
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.
0
 

Author Comment

by:murbro
ID: 39691733
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
0
 
LVL 16

Expert Comment

by:Chris Harte
ID: 39692410
That error message means it cannot start the activity MainActivity. Have you spelt it correctly and it is in the manifest xml?
0
 

Author Comment

by:murbro
ID: 39692576
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 }
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:Chris Harte
ID: 39692605
So, is it working then?
0
 

Author Comment

by:murbro
ID: 39692618
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?
0
 
LVL 16

Accepted Solution

by:
Chris Harte earned 500 total points
ID: 39692673
If you are using the eclipse IDE, which you should be, with the android ADT

http://developer.android.com/sdk/index.html

Then you can debug either through the Android Virtual Device Manager or on your connected device. Include the following line

android.os.Debug.waitForDebugger();

before the point at which you wish to set a breakpoint.

As an aside, I could never get the examples on the android site working, so I used this as my main tutorial

http://thenewboston.org/list.php?cat=6

Start at one and work your way through. A bit dated but nothing that is not solvable.
0
 

Author Closing Comment

by:murbro
ID: 39692693
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently asked a question (http://www.experts-exchange.com/Programming/Smartphones/Android/Q_28684946.html) about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
A short article about problems I had with the new location API and permissions in Marshmallow
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now