2011-05-09 103 views
5

我有代碼:findViewById返回null

public class HelloWorld extends Activity { 

private Button buttonOk; 
private Button buttonCancel; 

private OnClickListener buttonOkListener = new OnClickListener() { 
    public void onClick(View v){ 
     EditText editText = (EditText)findViewById(R.id.input); 

     CharSequence textFromInput = (CharSequence) editText.getText(); 
     Context context = getApplicationContext(); 
     int duration = Toast.LENGTH_SHORT; 

     Toast toast = Toast.makeText(context,textFromInput,duration); 
     toast.show(); 
    } 
}; 

private OnClickListener buttonCancelListener = new OnClickListener() { 
    public void onClick(View v){ 
     EditText editText = (EditText)findViewById(R.id.input); 

     CharSequence textFromInput = (CharSequence) editText.getText(); 
     Context context = getApplicationContext(); 
     int duration = Toast.LENGTH_SHORT; 

     Toast toast = Toast.makeText(context,textFromInput,duration); 
     toast.show(); 
    } 
}; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    // ToDo add your GUI initialization code here 

    setContentView(R.layout.main); 

    buttonOk = (Button)findViewById(R.id.buttonOk); 
    buttonCancel = (Button)findViewById(R.id.buttonCancel); 

    buttonOk.setOnClickListener(buttonOkListener); 
    buttonCancel.setOnClickListener(buttonCancelListener); 
} 
} 

和佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TextView 
    android:id="@+id/label" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Wpisz swoje imię:"/> 
<EditText 
    android:id="@+id/input" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:drawable/editbox_background" 
    android:layout_below="@id/label"/> 
<Button 
    android:id="@+id/buttonOk" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/input" 
    android:layout_alignParentRight="true" 
    android:layout_marginLeft="10dip" 
    android:text="OK" /> 
<Button 
    android:id="@+id/buttonCancel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@id/buttonOk" 
    android:layout_alignTop="@id/buttonOk" 
    android:text="Anuluj" /> 
</RelativeLayout> 

線與buttonCancel.setOnClickListener(buttonCancelListener);拋出異常,因爲buttonCancel爲null。我做錯了什麼?

+0

我面臨同樣的問題。即使代碼中的所有內容看起來都正確,但FindViewById將返回null與其中一個按鈕。 – castle1971 2011-10-26 07:48:05

回答

10

你的代碼沒有問題,通過正確的一切都應該正常工作。

通過findViewById方法遇到null最常見的錯誤是當您忘記調用setContentView或將其調用爲錯誤的佈局時。

我建議清理你的項目,然後再試一次!

1

我不是100%確定,但你正在調用findviewbyid的類初始化。我認爲這個代碼在onCreate方法之前被調用,所以無法找到視圖。初始化oncreate方法中的監聽器應該可以工作。

2

我遇到了同樣的麻煩,但在清理我的項目並再次運行後,它完美地工作。