views:

41

answers:

2

I am very new to Android Development. I am trying a sample application and it is generating a button dynamically using Java and it is working fine.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    btn=new Button(this);
    btn.setOnClickListener(this);
    updateTime();
    setContentView(btn);
    }

This works fine in my emulator. However when i try do with an XML based layout, my app crashes in the emulator.

Main.XML contents

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="com.testing"
    android:id="@+id/button"
    android:text=""
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

Code:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        btn=(Button)findViewById(R.id.button);
        btn.setOnClickListener(this);
        updateTime();

    }   

Does anyone know why this simple application is crashing because of the XML layout? Thanx a lot in advance :)

+1  A: 

Try:

<?xml version="1.0" encoding="utf-8"?>
<Button
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/button"
    android:text=""
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

Anyway... it will help if you post your logcat output.

Cristian
Yes! That seems to be the problem.... But i wonder why I cannot use my own namespace. Is this some sort of a constant namespace that cannot be changed? Or is it because this is linked with something that should be changed too in order for the application to work?
Ranhiru Cooray
+1  A: 

This line:

xmlns:android="com.testing"

should points to Android's XML Namespace... NOT your package name.

xmlns:android="http://schemas.android.com/apk/res/android"

You can, however rename the word android to something else provided that the attribute value remains the same.

chakrit
Thanx a lot. This is helpful :)
Ranhiru Cooray