我已经使用AlertDialog查看了有关此问题的所有帖子,但无济于事。有人能看到这里有什么不对劲吗?我尝试过DialogFragment,但是从PhoneStateListener执行此操作并且无法扩展其他任何内容。我没有空令牌,因此getBaseContext正在运行。我相信。
private void lookupCallerId(int cstate)
{
if(prefs.getIsDeactivated())
return;
if(lookupInProgress)
{
return;
}
//add popup box here for lookup question?
PMLog.v(LOGTAG, "lookupCallerId() Start pop up box.");
Context context = service.getBaseContext();
if(cstate == TelephonyManager.CALL_STATE_RINGING) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
PMLog.v(LOGTAG, "lookupCallerId() ALERT BUILDER.");
builder.setTitle("Lookup this #?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String strPhoneNumber = PhoneNumberProcessor.formattedPhoneNumber(prefs.getLastCallerNumber(), service); {
if(strPhoneNumber.length() == 0)
return;
}
PMLog.v(LOGTAG, "lookupCallerId() Starting CNM lookup thread");
Thread thread = new Thread(null, doBackgroundThreadProcessing, "LookupBackgroundThread");
thread.start();
}
}
);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
onNo();
return;
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
onNo();
return;
}
});
builder.create().show();
}
}
答案 0 :(得分:1)
问题归因于Context context = service.getBaseContext();
UI元素只能通过Activity上下文(即现有UI)添加。由于基本上下文没有与之关联的UI,因此您无法使用它向UI添加任何内容。
从活动中启动对话框,或使用Intent从服务中启动以对话为主题的对话框。