2010-02-21 3 views
2

AlertDialog 생성에 문제가 있습니다.AlertDialog 문제

하나의 버튼을 클릭하면 AlertDialog가 나타나기 때문에 버튼 리스너에 대화 상자를 만들기위한 모든 코드를 넣으십시오 ... 동일한 코드가 리스너 외부에서 작동하지만 리스너가 작동하지 않습니다 ... it

이가 내 코드입니다 .... "Aplication가 예기치 않게 종료되었습니다": 오류를 호출하지만 난 시작할 때 작성 에뮬레이터는 이것에 대해 뭔가하지 않습니다

btsenddata.setOnClickListener(new Button.OnClickListener() { 

     public void onClick (View v){ 

     AlertDialog.Builder alt_bld = new AlertDialog.Builder(getApplicationContext()); // I've tried with "activity1.this" instead "getApplicationContext()" but it is the same result 
     alt_bld.setMessage("Do you want to close this window ?") 
     .setCancelable(false) 
     .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
     // Action for 'Yes' Button 
     } 
     }) 
     .setNegativeButton("No", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
     //  Action for 'NO' Button 
     dialog.cancel(); 
     } 
     }); 
     AlertDialog alert = alt_bld.create(); 
     // Title for AlertDialog 
     alert.setTitle("Title"); 
     // Icon for AlertDialog 
     alert.setIcon(R.drawable.icon); 
     alert.show(); 
     }}); 
} 

.... 내가 그 메소드 getApplicationContext()가 몇 가지 문제를 만들고 있지만 activity.this로 대체하려고 시도했지만 어쨌든 작동하지 않는다고 들었습니다. 누군가가 이것에 대한 이유를 알고 있다면 alertdialogs는 답을줍니다. 감사합니다. .

답변

1

은 아마 당신이 당신에게 오류의 줄 번호를 알려줍니다 로그 캣에서 볼 수있는 스택 트레이스를 가지고 의미 "Aplication가 예기치 않게 종료되었습니다". 해당 스택 추적을보십시오. 이해할 수 없다면 원래 질문을 편집하여 여기에 게시하십시오.

0

나는 코드를 실행하고 MyActivity.yis를 사용합니다. 그리고 당신이 원하는 것은 대부분의 경우 활동 컨텍스트입니다. 이 blog post

당신은 또한 당신의 코드와 같이 v.getContext()

AlertDialog.Builder alt_bld = new AlertDialog.Builder(v.getContext());

0

으로 시도 할 수 있습니다 참조하십시오, 당신은 어떤 리스너에서 UI를 처리 할 수 ​​

주요 활동의 일부가 아닌 OnClickListener를,

을 구현 , Handler를 생성해야합니다. 다음 멤버 선언에 위의 코드

public Handler mHandler = new Handler(); 

쓰기, 리스너 코드 위

mHandler.post(new Runnable() { 
     public void run() { 
      // your UI handling code 
     } 
}); 

쓰기. 이것은 당신에게 문제를 해결할 수 있습니다.