2012-02-11 60 views
1

按鈕爲NULL的原因可能是什麼?Android Null按鈕

Button press; 
//... 

press = (Button) findViewById (R.id.enter); 

if (press != null) 
press.setOnClickListener(this); 

我檢查清單,XML文件,代碼...我沒有R.id.enter的任何重複,還是按鈕是空......這一切發生後,我公司推出了新的活動......

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    press = (Button) findViewById (R.id.enter); 

    if (press != null) 
    press.setOnClickListener(this); 
} 

這裏的XML(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" 
android:background="@drawable/gift2" 
> 

<Button 
android:id="@+id/enter" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_marginTop="415dp" 
android:background="@drawable/pattern" 
android:layout_gravity="center_horizontal" 
android:text="Open your box now!" 
android:textSize="20sp" 
/> 

</LinearLayout> 
+1

您是否使用佈局設置了內容視圖? – Rotemmiz 2012-02-11 20:55:23

+0

是的,我確實設置了它 – Madrugada 2012-02-11 20:57:59

+0

提交您的oncreate和佈局xml – Rotemmiz 2012-02-11 20:58:44

回答

1

一種可能是自動生成的R.java文件莫名其妙地得到了同步的或有無效值。

在這種情況下,R.id.enter可能不再有效,因此findViewById(R.id.enter)不能再找到要返回的有效對象。

如果使用Eclipse,請嘗試使用「Project - > Clean ...」 - 這將刪除所有自動生成的文件並重新創建它們。