tags:

views:

163

answers:

4

Hi, I am little bit ashamed to ask, but nevertheless...

I am a complete novice to Android, just installed the SDK and ADT Eclipse plugin. What I was trying to do is a simple "Hello, World" program. I am using the 2.2 (8) API.

Here's my code:

package com.example.hello;

import android.app.Activity; import android.widget.TextView; import android.os.Bundle;

public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView txt = new TextView(this);
        txt.setText("Hello, Android");
        setContentView(txt);

    }
}

My Eclipse console:

[2010-09-02 13:27:32 - HelloWorld] ------------------------------
[2010-09-02 13:27:32 - HelloWorld] Android Launch!
[2010-09-02 13:27:32 - HelloWorld] adb is running normally.
[2010-09-02 13:27:32 - HelloWorld] Performing com.example.hello.HelloWorld activity launch
[2010-09-02 13:27:32 - HelloWorld] Automatic Target Mode: Preferred AVD 'Android' is not available. Launching new emulator.
[2010-09-02 13:27:32 - HelloWorld] Launching a new emulator with Virtual Device 'Android'
[2010-09-02 13:28:12 - HelloWorld] New emulator found: emulator-5554
[2010-09-02 13:28:12 - HelloWorld] Waiting for HOME ('android.process.acore') to be launched...

And on my emulator (5554:Android) ... there is nothing displayed but blank screen with a cool fonted "Android" imprint.

What am I fundamentally missing ?

+1  A: 

Wait a while. What you're seeing is the emulator booting up.

Nathon
I do wait. In fact, my temp PC is quite slow (Athlon 1600). Anyway, I do wait quite some time - and I got the impression that nothing is loading. I will give another go, though, and wait a bit longer this time...
George
while waiting check your LogCat (eclipse -> view -> other -> android -> Logcat). If LogCat gives output, your emulator is still booting.
WarrenFaith
+1  A: 

yeah wait a bit. The emulator can take up to a few minutes to load especially if you have a slow machine...

Exile
Thanks a million! Just a few seconds ago, it did eventually load! Next time, I will pay closer attention to my console...
George
+1  A: 

Also, calling setContentView() twice is not ideal. Define your TextView in main.xml, and reference the text field from your code. Something like this:

TextView tv = (TextView)findViewById("textViewId");
tv.setText("Here goes the text!");
hpe
Thanks! I will keep that in mind!
George
+1  A: 

Yeah, the emulator takes its time loading, but as previously mentioned you've also got an issue in your code.

When you call

setContentView(R.layout.main);

you're essentially saying "go get the XML layout called 'main' that I made and display it"

Then after the fact you throw this in:

TextView txt = new TextView(this);
txt.setText("Hello, Android");
setContentView(txt);

In that code you're saying "create a new textview, set its text and then display the new layout I just made"

It's not really destructively erroneous, just not ideal. Typically you'll want to either create your layout ahead of time in XML or create your layout dynamically, but not both. Drawing a new view is one of the most CPU intensive actions that the average app performs and doing it twice is just wasteful.

If you're not familiar with the XML layout we're talking about, open your project in Eclipse and navigate to YourProjectName/res/layout/main.xml.

One last thing, it's probably not wise to build for a target platform of 2.2 unless there's something in the 2.2 API that you really really need. Your "Hello, world" app only uses a TextView, and those have been around forever. Building for 2.2 means that only users on 2.2 or higher can use your app, which at the moment excludes a lot of people (like all the Droid Eris users). Building for 1.5 is a pretty safe bet and still gives you a lot to play with while maximizing your target audience.

David Perry
Thanks, mate! Useful and interesting post!
George