简体   繁体   English

为什么在运行应用程序时出现此错误?

[英]Why am I getting this error when I run my app?

so I just wrote these lines of code: 所以我只写了以下几行代码:

package com.example.android.stimmenauswerter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    int nein = 0;
    int ja = 0;

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

    final TextView ergebnis =(TextView) findViewById(R.id.ergebnis);

    public void japluseins (View view)
    {
        ja++;
    }

    public void neinpluseins (View view)
    {
        nein++;
    }

    public void result (View view)
    {

        if(ja > nein)
        {
            ergebnis.setText(ja + " JA" + " vs " + nein + " NEIN" + "\nSomit hat JA gewonnen!");
        }
        else if(nein>ja)
        {
            ergebnis.setText(ja + " JA" + " vs " + nein + " NEIN" + "\nSomit hat NEIN gewonnen!");
        }
        else
        {
            ergebnis.setText(ja + " JA" + " vs " + nein + " NEIN" + "\nDas sieht mir nach einem Unentschieden aus!");
        }
    }
}

Basically the app should count the times you press yes or no and in the end print out the numbers. 基本上,应用程序应该计算您按“是”或“否”的时间,最后打印出数字。 I think the code would do exactly what I want it to do, but when i run the app it crashes printing out that errormessage: 我认为代码完全可以实现我想要的功能,但是当我运行该应用程序时,它会崩溃打印出该错误消息:

08-22 16:26:20.834 10574-10574/com.example.android.stimmenauswerter E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.example.android.stimmenauswerter, PID: 10574
                                                                                      java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.stimmenauswerter/com.example.android.stimmenauswerter.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3093)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
                                                                                          at android.app.ActivityThread.access$1100(ActivityThread.java:221)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                          at android.os.Looper.loop(Looper.java:158)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:7225)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                                          at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:72)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
                                                                                          at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:191)
                                                                                          at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)
                                                                                          at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)
                                                                                          at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:183)
                                                                                          at com.example.android.stimmenauswerter.MainActivity.<init>(MainActivity.java:21)
                                                                                          at java.lang.Class.newInstance(Native Method)
                                                                                          at android.app.Instrumentation.newActivity(Instrumentation.java:1095)
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3083)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
                                                                                          at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                          at android.os.Looper.loop(Looper.java:158) 
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:7225) 
                                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Can someone tell me what is wrong with the code and also might help me fixing it? 有人可以告诉我代码有什么问题,也可以帮助我解决它吗? Thank you in advance, Julian. 谢谢你,朱利安。

You are performing the following line in the wrong place: 您在错误的位置执行以下行:

final TextView ergebnis =(TextView) findViewById(R.id.ergebnis);

The above should be done inside the onCreate() method and after you have called setContentView() . 上述应的内部完成onCreate()方法,你都呼吁 setContentView()

The following is what it should look like: 以下是它的外观:

public class MainActivity extends AppCompatActivity {

int nein = 0;
int ja = 0;
TextView ergebnis;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ergebnis =(TextView) findViewById(R.id.ergebnis); // <--- This is the fix 
}

public void japluseins (View view)
{
    ja++;
}

public void neinpluseins (View view)
{
    nein++;
}

public void result (View view)
{

    if(ja > nein)
    {
        ergebnis.setText(ja + " JA" + " vs " + nein + " NEIN" + "\nSomit hat JA gewonnen!");
    }
}

final TextView ergebnis =(TextView) findViewById(R.id.ergebnis); will be null , any operation on this will result in NullPointerException . 将为null ,对此的任何操作都将导致NullPointerException It should be inside onCreate() 它应该在onCreate()里面

Your code should be something like: 您的代码应类似于:

 int nein = 0;
    int ja = 0;
     final TextView ergebnis = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       ergebnis =(TextView) findViewById(R.id.ergebnis);
    }

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

相关问题 为什么在运行程序时出现NullPointerException? - Why am I getting a NullPointerException when I run my program? 当我尝试在 Java 中运行我的套接字程序时,为什么会出现错误? - Why am I getting an error when I try to run my socket program in Java? 为什么在访问我的 Spring MVC 应用程序 URL 时出现 404 错误? - Why am I getting a 404 error when accessing my Spring MVC app URL? 我正在尝试运行我的应用程序,并且收到此错误消息? - i am trying to run my app and i am getting this error message? 为什么在尝试与我的服务器通信时出现此错误? - Why am i getting this error when trying to communicate with my server? 为什么在运行文件时出现此错误? - Why I'm getting this error when I run my file? 为什么我的for循环出现此错误? - Why am I getting this error with my for loop? 当我运行jsp页面时,没有显示图像。为什么? - I am not getting the display of images when i run my jsp page.why? 尝试运行JFrame时为什么会出现NullPointerException? - Why am I getting a NullPointerException when I try to run my JFrame? 当我在 localhost 上运行我的应用程序时,我能够在 html 上显示图像,但是当我部署到云平台时,图像显示为 404 - when I run my app on localhost am able to display images on html but when I deploy to a cloud platform am getting 404 for the images
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM