【问题标题】:Null pointer exception when casting from a service class to an activity从服务类转换为活动时出现空指针异常
【发布时间】:2013-07-09 21:37:20
【问题描述】:

我一直试图从 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;
    }       

}

我得到一个 NullPointerException ((DialogBox)activity.getApplicationContext()).onCreateDialog(id);

id 通过了,但我得到一个空活动。 是的,我对开发代码不太熟悉,所以我确定我遗漏了一些明显的东西。任何帮助将不胜感激。

【问题讨论】:

    标签: android android-activity dialog nullpointerexception


    【解决方案1】:

    首先:声明为静态的事物不包含有关对象特定实例的任何信息。它不是一种对象,因此您不能“投射”它。如果您需要从静态方法中的活动实例访问某些内容,请将实例传递给该方法。

    第二:你的静态接口定义在这个类中没有用到,可以去掉。如果您希望此类实际实现该接口,则需要在类声明中指定(公共类 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();
        }
    }
    

    那么,在activity中调用上述方法:

    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
        }
    }
    

    【讨论】:

      【解决方案2】:

      活动被初始化为 null 并且从未被赋值。这就是 NullPointerException 的来源。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多