0

Not a duplicate: my question is simpler than all of the others.

I've been trying to follow the android hello world tutorial, and I can't get the very first example to work.

This is my code:

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);
    }
}

As you can see, I copied and pasted directly out of the tutorial. The problem is, that instead of displaying Hello, Android, it displays whatever is in the layout/main.xml file. If that file doesn't exist, it closes without displaying anything.

WHY IS THIS NOT WORKING?

As I've copied this directly from the official docs, I have no idea where to even start trying to debug it. Any pointers or suggestions you can give will be greatly appreciated!

Edit: posting my main.xml as requested

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, HelloAndroid"
    />
</LinearLayout>

Note that this was created automatically when I started the project, I didn't put it there.

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
Benubird
  • 18,551
  • 27
  • 90
  • 141
  • If you've created that file, it means you weren't following the tutorial in a linear fashion or have gone back to a previous step. Clean and re-build your project. – Brian Roach Jan 21 '12 at 16:20
  • Also, can you post your main.xml as well? – Jrom Jan 21 '12 at 16:23
  • @Brian I didn't create that file; it was automatically created when I started a new project. – Benubird Jan 21 '12 at 16:24
  • Have you tried to set a breakpoint in the onCreate method to see if it is really called? From what I can see it should work. Maybe your project wasn't uploaded correctly to the emulator. – Matthias Schippling Jan 21 '12 at 16:38
  • 1
    I just tried your example with your code and it seems to display the text correctly (dynamically, not from main.xml). I would recommend you go through the steps again and make sure you didn't miss anything. – Jrom Jan 21 '12 at 16:39
  • Update: this also does nothing: addContentView(tv, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); – Benubird Jan 21 '12 at 16:46
  • @Matthias how do I set a breakpoint? – Benubird Jan 21 '12 at 16:48
  • I guess you're using Eclipse. In that case you should see a vertical line before your code. At the moment it will most likely only show a green triangle in front of your method head (because of your "Override" attribute). Double-click on this vertical line wherever you want to set a breakpoint or right-click and choose "Toggle breakpoint". Remember to click on "Debug As Android Application" instead of "Run As". – Matthias Schippling Jan 21 '12 at 16:57
  • Btw.: if you right click on this vertical line you can also activate "Show Line Numbers". I find it very helpful and it's the first thing I do when I install Eclipse :) – Matthias Schippling Jan 21 '12 at 17:00
  • @Matthias thanks, but I don't use eclipse. I'm trying to figure out how to connect gdb to the emulator now to debug it, I'll let you know if I can , but this is getting away a bit from the original question - does anyone know how to make the tutorial work? – Benubird Jan 21 '12 at 17:04

4 Answers4

2

Why do you have two TextViews with the same text? You shouldn't be doing this, if you're only going to use one, then only use one.

You XML is fine as is, but your Activity code needs to change:

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Heres an explanation of what you were doing...

You were building a TextView dynamically and setting the ContentView to that TextView is fine, but it was not your original intention. Your original intention or the original intention of the sample was to use the layout file and then set the contentview of that activity to that layout file, not just that textview.

Update

In lieu of what the OP said, he does want to build a TextView instance dynamically and apply it to the screen. If this is the case then you have to...:

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.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT));
        tv.setText("Hello, Android");
        setContentView(tv);
    }
}

The only difference here is that the TextView has properties that are defining how it will appear on the screen. You can either user FILL_PARENT or WRAP_CONTENT.

jameshfisher
  • 34,029
  • 31
  • 121
  • 167
JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • I don't want to use the xml, because I need to be able to set the content dynamically. "building a TextView dynamically and setting the ContentView to that TextView" is EXACTLY what I want to do, but I can't figure out how to do it. – Benubird Jan 21 '12 at 17:01
  • what is the error? then why do you have a main.xml? If you're setting the content, then it doens't matter. However; you do have to to define the layout parameters of that TextView. otherwise the WindowManager won't know how to draw it and it WILL COMPLAIN. – JoxTraex Jan 21 '12 at 17:03
  • Any time you are posting on stackoverflow you NEED to post your logcat error, so that we can see what the error is. Its not enough to say "its crashing" we have to know why. This information is in the logs. – JoxTraex Jan 21 '12 at 17:11
  • I have main.xml because it was there by default when I created the project - like I said, I didn't put it there; I was just following the tutorial, doing exactly what it said. What is logcat? I can post the log if you want, but it's really long. My program isn't crashing, it's just not doing what I want it to do (and what the tutorial said it would be doing). – Benubird Jan 21 '12 at 17:16
  • What do you expect that it should be doing? All it SHOULD be doing is displaying the Text hello world. – JoxTraex Jan 21 '12 at 17:17
  • yes, what is SHOULD be doing is displaying the text "Hello, Android". It's not. It is instead displaying the text "Hello World, HelloAndroid". The question is: Why? – Benubird Jan 21 '12 at 23:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6932/discussion-between-joxtraex-and-benubird) – JoxTraex Jan 22 '12 at 00:12
1

you haven't set the xml file or layout in your application. You are directly calling the view.You need to call your view under layout.

Syntax: setContentView(R.layout.your xml file name);

Ranjit Mishra
  • 440
  • 4
  • 8
0

Regarding the question about logcat, you'll find it in the Debug perspective in Eclipse. It's the log of error messages from system and user code.

Don Cowan
  • 1
  • 3
0

Try this: You don't need to do setContentView at end. If you do that, it will override with what you have in XML.

@Override     
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourxmlfile);
    TextView tv = (TextView)findViewById(R.id.helloText);
    tv.setText("Hello, Android");
}
<TextView
    android:id="@+id/helloText"
    android:layout_width="fill_parent"    
    android:layout_height="wrap_content"
    android:text="Hello World, HelloAndroid" />
SapuSeven
  • 1,473
  • 17
  • 30
kosa
  • 65,990
  • 13
  • 130
  • 167
  • Doesn't help. 1) have to define tv before I can use it as an argument, 2) tried putting it between the definition and settext, didn't do anything. – Benubird Jan 21 '12 at 16:20
  • Sorry! my mistake. I have updated my answer with correct syntax. – kosa Jan 21 '12 at 16:23
  • hold on, isn't that doing exactly what I want it not to do? It looks like what you've put there is what it already does, that I am trying to find out how to make it not do, which is to say read from an xml file instead of generating programmatically. – Benubird Jan 21 '12 at 16:28
  • If you want to display content from your main.xml, don't need to add last two lines of code. In other words, if you want to see "Hello, Android", use exact code in my answer. If you want to see "Hello World, HelloAndroid", remove last two lines from my answer. – kosa Jan 21 '12 at 16:32
  • @Benubird: You're right, the answer not correct. The first two lines load the layout from the xml file, the last two lines create a TextView that is removed immediately because no reference to it exists. – Matthias Schippling Jan 21 '12 at 16:34
  • @thinksteep: Your code won't show "Hello, Android", because the new TextView is not set as the content view of the Activity. – Matthias Schippling Jan 21 '12 at 16:36
  • I have edited my answer again, you need to have id for your your textView in xml. With that Id, you can override what ever you have in xml in java program. – kosa Jan 21 '12 at 16:37
  • @Matthias: I Just edited my answer with more accurate content, as you are typing. OP XML missing id for textview too. Anyways, thanks for pointing. – kosa Jan 21 '12 at 16:38
  • @thinksteep you seem to have misread my question I want to make it NOT show R.layout.yourxmlfile, only the textview. Your answer does the OPPOSITE of what I want. – Benubird Jan 21 '12 at 17:01
  • I might misread. Let me try to clarify. If you don't want to have TextView in xml and get it generated in java program, just remove text view from your xml file and do like this in this discussion http://stackoverflow.com/questions/3204852/android-add-a-textview-to-linear-layout-programmatically. If you want to have textview in xml, but just override its value in java program, just try exact same like my answer you have now (check my answer now, i did couple of revisions). – kosa Jan 21 '12 at 17:06