onClickListener 배우고 있는데 예제파일은 실행이 되지만 제가
직접 코딩하면 실행이 되지 않습니다.
package work.test.keyeventtest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class KeyEventTestMainActivity extends Activity {
static final int ENDDIALOG = 0;
static final int SELECTDIALOG = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_key_event_test_main);
btnset();
//"종료하려면 back 버튼을 클릭하세요"메시지를 표시하는 토스트가 표시
Toast.makeText(this, R.string.messageStr,
Toast.LENGTH_SHORT).show();
}
private void btnset() {
// TODO Auto-generated method stub
Button select = (Button)findViewById(R.id.selectBtn);
select.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(SELECTDIALOG);
}
});
}
//버튼을 누르면 실행됨
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK)//누튼 버튼이 back 버튼이면
showDialog(ENDDIALOG);//대화상자가 표시
return true;
}
protected Dialog onCreateDialog(int id) {//대화상자 생성
Dialog dialog = null;
switch(id) {
//대화상자의 ID가 ENDDIALOG인 경우
case ENDDIALOG:
dialog = process();//메소드 호출
break;
default:
dialog = null;
}
return dialog;
}
//대화상자를 생성하는 메소드
public Dialog process(){
//대화상자에 필요한 속성을 지정하기 위해 Builder생성
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("종료하시겠습니까?") //메시지
.setTitle(R.string.app_name) //타이틀
.setPositiveButton(R.string.pStr, //버튼레이블
//대화상자의 [확인]버튼에 리스너등록
new DialogInterface.OnClickListener() {
//대화상자의 [확인]버튼을 누르면 실행
public void onClick(DialogInterface dialog, int id) {
//애플리케이션 종료
KeyEventTestMainActivity.this.finish();
}
})
.setNegativeButton(R.string.nStr, //버튼레이블
//대화상자의 [취소]버튼에 리스너등록
new DialogInterface.OnClickListener() {
//대화상자의 [취소]버튼을 누르면 실행
public void onClick(DialogInterface dialog, int id) {
dialog.cancel(); //대화상자 취소
}
});
// AlertDialog객체 생성해서 호출한 곳으로 리턴
return builder.create();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_key_event_test_main, menu);
return true;
}
}
--------------------------------------------------------------------------------------------------------
여기서 빨간색으로 코딩한부분만 추가하면 자꾸 실행이 되지 않는데요.
에러 로그는 다음과같습니다.