[android] AlertDialog - You need to use a Theme.AppCompat theme (or descendant) with this activity.

북마크 추가

MainActivity에서 Context를 선언하고

 

private Context mContext;

 

onCreate안에서 applicationContext를 받아서

mContext=this.getApplicationContext();

 

 

별도의 클래스로 선언한 WebChromeClient에서 사용하려고 하니

 

밑줄친 부분에서 You need to use a Theme.AppCompat theme (or descendant) with this activity. 에러가 발생한다.

class UriChromeClient extends WebChromeClient {

@Override
public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
{
new AlertDialog.Builder(mContext).setTitle("AlertDialog").setMessage(message)
.setPositiveButton(android.R.string.ok,new AlertDialog.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
result.confirm();
}
}).setCancelable(false).create().show();
return true;
}
}

-> 해결방법 위 코드의 mContext를 MainActivity.this 로 바꿔준다.

class UriChromeClient extends WebChromeClient {

@Override
public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
{
new AlertDialog.Builder(MainActivity.this).setTitle("AlertDialog").setMessage(message)
.setPositiveButton(android.R.string.ok,new AlertDialog.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
result.confirm();
}
}).setCancelable(false).create().show();
return true;
}
}

 

 

 

 

 

 

 

AD
관리자
2016-05-19 13:10
SHARE