我一直在尝试从PhoneListener服务类弹出一个对话框。我附带了一个对话框活动类。我正试图在呼叫状态更改时弹出对话框。我试过从静态中选角,显然我一点也不懂静态。我似乎永远无法获得AlertDiag.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((DialogBox)activity.getApplicationContext()).onCreateDialog(id);
id通过了,但我得到了一个null活动。是的,我对开发代码不是很熟悉,所以我确信我遗漏了一些显而易见的东西。如有任何帮助,我们将不胜感激。
第一:声明为静态的东西不包含任何关于对象的特定实例的信息。它不是一种类型的对象,因此您不能"强制转换"它。如果您需要从静态方法中的活动实例访问某个内容,请将该实例传递给该方法。
第二:这个类中没有使用静态接口定义,可以删除它。如果你想让这个类真正实现那个接口,你需要在类声明中指定它(公共类DialogBox扩展了Activity实现了DialogBoxPopUp)。
第三:由于您的类(DialogBox)扩展了Activity对象,所以这是您通常获得上下文的地方。
第四:这个类不应该被声明为抽象的。
删除变量"activity"——您正在将其初始化为null,而不是其他任何内容,因此它永远不会有上下文。
但我认为你想要的是:一个可以帮助你构建对话框的类。如果是这种情况,您可以使这些方法成为静态的,但您需要将有效的上下文传递给该静态方法(我还没有运行或编译过它,所以将其视为伪代码):
public class MyDialogBox{
private MyDialogBox(){} //private constructor so this class can't be instantiated
public static void ShowDialog(Context c, OnClickListener onYesClick,
OnClickListener onNoClick, OnCancelListener onCancel){
AlertDialog.Builder builder=new AlertDialog.Builder(c);
builder
.setTitle("Privus Mobile")
.setMessage("Lookup this number?")
.setPositiveButton(R.string.yes, onYesClick)
.setNegativeButton(R.string.no, onNoClick)
.setOnCancelListener(onCancel)
.show();
}
}
然后,在调用上述方法的活动中:
public class MyActivity extends Activity {
//normal implementation code
public void SomethingHappenedShowMyDialog(){
MyDialogBox.ShowDialog(
this, //"this" refers to this activity, and activity extends a context
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyActivity.this.onYes(); //call the defined method
}
},
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//or just define it here
Toast.makeText(MyActivity.this, "No Clicked", Toast.LENGTH_SHORT).show();
}
},
new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog){
//do something
}
});
}
public void onYes(){
//do something
}
}
活动被初始化为null,并且从不分配值。这就是NullPointerException的来源。