简体   繁体   English

Android Studio - 错误:应用程序在虚拟设备上崩溃

[英]Android Studio - Error: App is crashing on Virtual Device

So, I am a new programmer and also new here on this platform.所以,我是一个新的程序员,也是这个平台上的新人。 I have got a Problem.我有一个问题。 I tried everything like to watch Videos, read older Posts on StackOverflow, but nothing helped me really.我尝试了所有方法,例如观看视频、阅读 StackOverflow 上的旧帖子,但没有任何帮助。 That's why I wanted to ask you for help.这就是为什么我想请你帮忙。

This is my Logcat after the crash report:这是崩溃报告后我的 Logcat:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at de.S8S7.Spiel.onCreate(Spiel.java:32)引起:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at de.S8S7.Spiel.onCreate(Spiel.java) :32)

And at this point it Shows the error:此时它显示错误:

btnAntwortEins.setOnClickListener(this);
btnAntwortZwei.setOnClickListener(this);
btnAntwortDrei.setOnClickListener(this);
btnAntwortVier.setOnClickListener(this);

And yeah, I am not native English speaker.是的,我的母语不是英语。

Beforde you can set the OnClickListeners, you have to load the instances of the Buttons like this:之前你可以设置 OnClickListeners,你必须像这样加载 Buttons 的实例:

btnAntwortEins = findViewById(R.id.btnIdAntwortEins);
btnAntwortZwei = findViewById(R.id.btnIdAntwortZwei);
btnAntwortDrei = findViewById(R.id.btnIdAntwortDrei);
btnAntwortVier = findViewById(R.id.btnIdAntwortVier);

btnAntwortEins.setOnClickListener(this);
btnAntwortZwei.setOnClickListener(this);
btnAntwortDrei.setOnClickListener(this);
btnAntwortVier.setOnClickListener(this);

The Id (eg btnIdAntwortEins) is what you have written into the Id field of each Button in the layout editor. Id(例如 btnIdAntwortEins)是您在布局编辑器中写入每个 Button 的 Id 字段的内容。

if its only crashing in virtual device then may be u have used google play service in your app and you have not istalled a emulator with play servivce.如果它只是在虚拟设备中崩溃,那么可能是你在你的应用程序中使用了谷歌播放服务并且你没有安装带有播放服务的模拟器。 try to instyall emulator with play services.尝试使用播放服务安装模拟器。

在此处输入图片说明

There might be a scenario of having such crash when you dont intialize the button correctly in android.So as you are new to Android development consider these things when you initialize the buttons in the Activity当您没有在 android 中正确初始化按钮时,可能会出现这种崩溃的情况。因此,当您是 Android 开发新手时,请在 Activity 中初始化按钮时考虑这些事情

You can mention the Button Globally like:您可以全局提及按钮,例如:

Button button;

you have to give the reference id which you have defined in the xml like;您必须提供您在 xml 中定义的参考 ID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    githubRepoUrlRecyclerView = findViewById(R.id.button);

//mention on click listener after you have given the reference id.

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO task.
            }
        });

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

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