点击视图时我试图打开一个对话框

时间:2018-09-08 05:20:55

标签: android

点击视图时,我试图打开一个对话框。 MainActivity类:

public class MainActivity extends AppCompatActivity {
    DialogInterface alertDialog1;
    ...
    public  void cangrats(){
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("Select Your Choice");
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                builder.setCancelable(true);
            }});
        alertDialog1 = builder.create();
        builder.show();
    }
}

,迷宫课:

public class maze extends View {
    MainActivity mainActivity = new MainActivity();

    public maze(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            mainActivity.cangrats();
        }

        return true;
    }
}

,错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.blah.blah.myapplication, PID: 4435
                  java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
                      at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:149)
                      at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
                      at android.support.v7.app.AlertDialog.resolveDialogTheme(AlertDialog.java:114)
                      at android.support.v7.app.AlertDialog$Builder.<init>(AlertDialog.java:294)
                      at com.blah.blah.myapplication.MainActivity.cangrats(MainActivity.java:17)
                      at com.blah.blah.myapplication.maze.onTouchEvent(maze.java:25)
                      at android.view.View.dispatchTouchEvent(View.java:9943)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2301)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2301)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2301)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2301)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2301)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2301)
                      at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:411)
                      at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1810)
                      at android.app.Activity.dispatchTouchEvent(Activity.java:3061)
                      at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
                      at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:373)
                      at android.view.View.dispatchPointerEvent(View.java:10163)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4434)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4302)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
                      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3995)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
                      at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4052)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
                      at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6210)
                      at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6184)
                      at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6145)
                      at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6313)
                      at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                      at android.os.MessageQueue.nativePollOnce(Native Method)
                      at android.os.MessageQueue.next(MessageQueue.java:323)
                      at android.os.Looper.loop(Looper.java:136)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

3 个答案:

答案 0 :(得分:0)

进行以下修改:

在你maze.java

  public class maze extends View {
  MainActivity mainActivity; // do not instantiate mainActivity object
public maze(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        if(mainActivity==null)
         return true;
        mainActivity.cangrats();
    }

    return true;
}
 // Create a method to pass MainActivity reference to maze
 public void setMainActivity(MainActivity mainActivity)
 {
           this.mainActivity=mainActivity;
 }
}

在MainActivity的onCreate方法调用setMainActivity方法中

mazeObj.setMainActivity(this);

答案 1 :(得分:0)

此代码有很多错误,首先不要创建如下活动:

MainActivity mainActivity = new MainActivity();

此代码不会创建可用的活动,该活动将没有上下文,因为它没有附加到应用程序进程中,该活动也不会开始,它只是一个空容器!

如果您要创建并开始一项活动,则必须使用意图来询问ActivityManager,如下所示:

context.startActivity(new Intent(context, MainActivity.class));

第二次builder.create()返回AlertDialog,不要将其强制转换为DialogInterface

最近builder.setCancelable(true)将不起作用!显示对话框后,如果您想关闭该对话框,构建器作业将结束,因为DialogInterface.OnClickListener默认情况下会关闭该对话框,所以您无需执行任何操作。

答案 2 :(得分:0)

You can you use static method for this and show your dialog
like this and also create your variable like this

 AlertDialog alertDialog1

 public static void cangrats(){
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Select Your Choice");
    builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            builder.setCancelable(true);
        }});
    alertDialog1 = builder.create();
    builder.show();
}

,然后在您的视图类中使用诸如MainActivity.cangrats();