2010-09-14 28 views
0

我對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佈局的崩潰? 感謝名單很多提前:)

回答

1

嘗試:

<?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輸出幫助。

+0

是的!這似乎是問題....但我想知道爲什麼我不能使用我自己的命名空間。這是某種不能改變的恆定名稱空間嗎?或者是因爲這與應該改變的東西有關,以便應用程序正常工作? – 2010-09-14 18:37:14

1

這條線:

xmlns:android="com.testing" 

應該指向Android的XML命名空間...... 你的包名。

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

但是,您可以將單詞android重新命名爲其他屬性,只要該屬性值保持不變即可。

+0

非常感謝。這是有幫助的:) – 2010-09-14 18:43:19

相關問題