Hi,
I have followed the android hello world example, I followed exactly
all the steps, but got a different result.
According to the example, the result should be a window with the title
of "Hello, Android", and the content of "Hello, Android" as well. But
what I got is a title "Hello, Android" and the content of "Hello
World, HelloAndroid". I tried to put other strings in tv.setText(),
but it never shows up. looks like the content of the window is always
the class name following "Hello World," but where the hell does the
"Hello World" string comes from? There's only a "Hello Android" in the
java code.
Help is highly appreciated.
Here is the code:
<code>
package com.google.android.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(R.layout.main);
}
}
</code>
Hua Yanghao
I think you should setContentView to tv not R.layout.main.
My problem is when i run the application, the emulator starts, but nothing is displayed.. who can help?
jandfus is correct. The problem is that the TextView you declared is never used since you set the layout to the one explicitly declared in the resources. To change the text on THIS textbox, you need something like
TextEdit te = (TextEdit) findViewById(R.id.myTextEdit);
te.setText("Yay!");
There are 2 ways to set your layout, you can do this by programming it all, like in your code, or you can create an .xml file wich specifies your layout. Check res/layout for that. To be able to use this xml layout, there's R.java wich provides a "link" to your layout. This is what you did with
And so, your java-layout gets replaced. It is recommend that you use, the xml layout, it allows for easy editing. You can then change the text in your code if you really need to.Code:setContentView(R.layout.main);
Ishtar
I see a pink phone with "ANDROID" displayed on the phone and a qwerty keyboard displayed to the right of the phone...My problem is when i run the application, the emulator starts, but nothing is displayed.. who can help?
any suggestions....?
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}
I went back to the code and tried to run it again and it worked.... I have no idea what I did to make it work but thanks anyway....![]()
I made the same mistake - I failed to change
<code>
setContentView(R.layout.main);
</code>
to
<code>
setContentView(tv);
</code>
Which is what the starting guide tells you to do. Now all I need is a guide for writting "text" applications se I can learn about data and general java language before getting all stuck in GUI land. Thanks
Bookmarks