我一直在尝试从 PhoneListener 服务类中弹出一个对话框。我有一个对话框活动类附加。当呼叫状态发生变化时,我试图弹出对话框。我尝试过从静态转换,显然我根本不懂静态。我似乎永远无法获得 AlerDialg.Builder 的活动或上下文。这是我在 PHoneListener 类中的调用:
DialogBox.onCreateDialog2(1);
这是对话框代码:
public abstract class DialogBox extends Activity {
static abstract interface DialogBoxPopUp {
void onCreateDialog(int id);
void onCreateDialog2(int id);
}
Dialog dialog = null;
int DIALOG_X = 1;
int DIALOG_Y = 2;
int DIALOG_Z = 3;
private static Activity activity = null;
private static final String LOGTAG = "DialogBoxPopUp";
AlertDialog alertDialog;
public Dialog onCreateDialog(int id) {
switch(id) {
case 1:
// do the work to define the X Dialog
AlertDialog.Builder builder=new AlertDialog.Builder(activity.getParent());
PMLog.d(LOGTAG, "Got to PopUp, have an activity?");
builder
.setTitle("Privus Mobile")
.setMessage("Lookup this number?")
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
onYes();
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
onNo();
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener()
{
public void onCancel(DialogInterface dialog)
{
onNo();
}
})
.show();
PMLog.d(LOGTAG, "Got to show");
break;
default:
dialog = null;
}
return dialog;
}
public static void onYes() {
PrivusPhoneStateListener.lookupCallerId();
}
public static void onNo() {
return;
}
public static Dialog onCreateDialog2(int id) {
((DialogBox) activity.getApplicationContext()).onCreateDialog(id);
return null;
}
}
我在 ((DialogBox)activity.getApplicationContext()).onCreateDialog(id); 上得到一个 NullPointerException
id 通过了,但我得到了一个空活动。是的,我对开发代码不太熟悉,所以我确定我遗漏了一些明显的东西。任何帮助将不胜感激。