在下面的代碼段構建時,我將OnClickListener添加到按鈕時保持中斷狀態。Android setOnClickListener錯誤
package xxx.xxx.xxx;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class TeaActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Button AddItem = (Button)findViewById(R.id.button1);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AddItem.setOnClickListener(AddButtonHandler);
}
View.OnClickListener AddButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
// it was the first button
}
};
調試代碼時會中斷setOnClickListener()調用。不知道爲什麼
你不應該在'setContentView'之前調用'findViewById'。 – adneal