設置OnClickListener當我在Android的初學者,我寫了下面簡單的程序,只是顯示的按鈕,我按一個TextView的文本(我總是得到一個運行時錯誤):錯誤的Android
package com.hmz.secondapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AppPageActivity extends Activity {
Button btnPlus;
Button btnMinus;
Button btnMul;
Button btnDiv;
TextView resultTextView;
View.OnClickListener operations = new View.OnClickListener() {
public void onClick(View v) {
Button pressedButton = (Button) v;
String buttonLabel = pressedButton.getText().toString();
resultTextView.setText(buttonLabel);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_page);
btnPlus.setOnClickListener(operations);
btnMinus.setOnClickListener(operations);
btnMul.setOnClickListener(operations);
btnDiv.setOnClickListener(operations);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.app_page, menu);
return true;
}
}
發佈錯誤日誌。 – jeet