Reputation: 3807
I just installed the SDK
and the 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 a blank screen with a cool fonted "Android" imprint. What am I fundamentally missing ?
Upvotes: 2
Views: 14132
Reputation: 1
I also got the same problem then. Wait until it download the emulator, then click on the lock button of android and then rotate the circle in clock wise and then opens the android application. Then if you click on the main option in the android then you can dinf your project name there click on that, then you will see the output there.
Upvotes: 0
Reputation: 3228
After calling
setContentView(R.layout.main);
You did not need to use it again for Setting text
to textview
. You can use it like this.
TextView tv = new TextView(R.id.textview1);
tv.setText("Hello, Android");
It will work.
Upvotes: 0
Reputation: 96
I had this very problem and spent like a whole day on the net searching for the solution. Eclipse console log had stopped at:
[2011-04-02 10:32:57 - MMarketSurvey] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.okune.mmarketsurvey/.mmarketsurvey }
I visited this very page but I did not get any solution for the problem. I almost gave up but still felt it's not something big because everything seemed ok, it's just that I could not get the hello android text on the emulator. It's very frustrating when a small thing doesnt seem to work when you have done every other thing right.
To quote shodhanshah in this forum thread:
Everything was fine however emulator was on home screen and application was running in background . I switched to application screen and found that app was running fine.
Just press home on keyboard, click apps on screen, then click your application name.
Upvotes: 1
Reputation: 1364
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.
Upvotes: 3
Reputation:
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!");
Upvotes: 1
Reputation: 9603
yeah wait a bit. The emulator can take up to a few minutes to load especially if you have a slow machine...
Upvotes: 1