我一直在嘗試從PhoneListener服務類中彈出一個對話框。我有一個DialogBox活動類附加。我試圖在通話狀態改變時彈出對話框。我已經嘗試從靜態鑄造,顯然我不明白靜態。我永遠無法得到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;
}
}
我得到一個NullPointerException ((對話框)activity.getApplicationContext()) .onCreateDialog(ID);
該ID通過,但我得到一個空活動。 是的,我不太熟悉開發代碼,所以我肯定我錯過了一些明顯的東西。任何幫助將不勝感激。