[英]Simple Android App Not Responding
在我為按鈕分配ID之前,該應用程序運行良好,然后開始變得無響應。 該代碼是最少的,但是我在另一個小型測試項目中遇到了同樣的事情。 問題是我不理解為什么它不起作用,尤其是如何解決它,即使我做了所有的互聯網搜索也是如此。 我剛剛開始使用Android基礎知識,因此如果錯誤看起來過於簡單,我深表歉意。 任何幫助是極大的贊賞。 謝謝。
public class Test extends Activity
{
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button m = (Button) findViewById(R.id.test);
m.setOnClickListener
(
new View.OnClickListener()
{
public void onClick(View v)
{
// some code
}
}
);
}
}
這是main.xml
<?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"
>
<Button
android:id="@+id/test"
android:text="asdas"
></Button>
</LinearLayout>
您還需要android:layout_width & height
屬性來設置按鈕,我不確定您是否可以按照現在的方式進行編譯。
至少缺少android:layout_width
和android:layout:height
,因此按鈕無法顯示。 您可以做的是使用android sdk為Eclipse提供的圖形布局編輯器,從那里添加一個按鈕,然后觀察xml的構建方式以完全理解所有參數。
您需要指定按鈕的寬度和高度。
例:
<Button
android:text="Click"
android:layout_width="100dp"
android:layout_height="50dp" />
</Button>
dp(dip)是密度獨立像素: 更多
沒有這些屬性,應用程序將編譯,但以錯誤結束:
Unable to start activity ComponentInfo... You must supply a layout_width attribute.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.