简体   繁体   English

为什么这个基于XML布局的Android应用程序崩溃?

[英]Why does this XML-Layout based Android Application crash?

I am very new to Android Development. 我是Android开发的新手。 I am trying a sample application and it is generating a button dynamically using Java and it is working fine. 我正在尝试一个示例应用程序,它正在使用Java动态生成按钮,并且运行良好。

    @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. 但是,当我尝试使用基于XML的布局时,我的应用在模拟器中崩溃。

Main.XML contents 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" />

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? 有谁知道为什么这个简单的应用程序由于XML布局而崩溃? Thanx a lot in advance :) 非常感谢:)

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. 无论如何...如果您发布logcat输出,这将有所帮助。

This line: 这一行:

xmlns:android="com.testing"

should points to Android's XML Namespace... NOT your package name. 应该指向Android的XML命名空间...而不是您的包名称。

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. 您可以将android一词重命名为其他名称,前提是属性值保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM