为什么这个基于XML布局的Android应用程序崩溃?
问题描述:
我对Android开发非常陌生。我正在尝试一个示例应用程序,它使用Java动态生成一个按钮,它工作正常。为什么这个基于XML布局的Android应用程序崩溃?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btn=new Button(this);
btn.setOnClickListener(this);
updateTime();
setContentView(btn);
}
这在我的模拟器中正常工作。但是,当我尝试使用基于XML的布局时,我的应用程序在模拟器中崩溃。
Main.XML
内容
<?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" />
代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(this);
updateTime();
}
有谁知道为什么这个简单的应用程序,因为XML布局的崩溃? 感谢名单很多提前:)
答
尝试:
<?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" />
反正...这将如果您发布的logcat输出帮助。
答
这条线:
xmlns:android="com.testing"
应该指向Android的XML命名空间...... 不你的包名。
xmlns:android="http://schemas.android.com/apk/res/android"
但是,您可以将单词android
重新命名为其他属性,只要该属性值保持不变即可。
+0
非常感谢。这是有帮助的:) – 2010-09-14 18:43:19
是的!这似乎是问题....但我想知道为什么我不能使用我自己的命名空间。这是某种不能改变的恒定名称空间吗?或者是因为这与应该改变的东西有关,以便应用程序正常工作? – 2010-09-14 18:37:14