简体   繁体   English

警报对话框不起作用,强制在启动时关闭

[英]Alert Dialog doesn't work, force close on launch

My AlertDialog doesn't work, the app closes on launch. 我的AlertDialog不起作用,该应用在启动时关闭。

Button btn_disclaimer = (Button) findViewById(R.id.btn_disclaimer);
    btn_disclaimer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MyActivity.this);
            alertDialogBuilder.setTitle("DISCLAIMER");
            alertDialogBuilder
                    .setMessage("TEST123")
                    .setNegativeButton("Okay", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();

                                }
                            });

            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();


        }
    });

What is wrong with it? 怎么了 I imported these guys: 我导入了这些家伙:

import android.app.AlertDialog;
import android.content.DialogInterface;

I'm new to Android, just creating some simple apps. 我是Android的新手,只是创建了一些简单的应用程序。

java.lang.RuntimeException: Unable to start activity ComponentInfo{gd.mkp.jd.de.gymnasiumdamme/gd.mkp.jd.de.gymnasiumdamme.MyActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2216)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5140)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at gd.mkp.jd.de.gymnasiumdamme.MyActivity.onCreate(MyActivity.java:279)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2170)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5140)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
            at dalvik.system.NativeStart.main(Native Method)

To me it looks like your btn_disclaim object is null. 在我看来,您的btn_disclaim对象为null。 Reasons: 原因:

1) You use the wrong id when doing the findViewById(R.id.btn_disclaimer). 1)执行findViewById(R.id.btn_disclaimer)时使用错误的ID。 Make sure the button you're trying to reference actually has this id. 确保您要引用的按钮实际上具有此ID。

2) The button XML layout you are trying to reference is not actually in the XML layout you are loading with this Activity. 2)您尝试引用的按钮XML布局实际上不在您通过此活动加载的XML布局中。 The id exists, and so you can compile the app, but the view in which it exists is not the one you are loading using setContentView(R.layout.whatever_your_activity_layout_is). 该ID存在,因此您可以编译该应用,但是它所在的视图不是使用setContentView(R.layout.whatever_your_activity_layout_is)加载的视图。 Make sure you are loading the correct layout, or make sure your button layout is actually defined in the layout you are load for this Activity. 确保您正在加载正确的布局,或确保在为此活动加载的布局中实际定义了按钮布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM