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

时间:2016-08-22 14:37:15

标签: java android variables runtime-error counting

所以我刚写了这些代码:

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!");
        }
    }
}

基本上,应用程序应计算您按是或否的次数,最后打印出数字。我认为代码将完全按照我的要求执行,但是当我运行应用程序时,它会崩溃打印出错误消息:

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)

有人可以告诉我代码有什么问题,也可以帮我解决问题吗? 先感谢您, 朱利安。

2 个答案:

答案 0 :(得分:1)

您在错误的位置执行以下行:

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

上述内容应在onCreate()方法内完成,调用setContentView()后完成。

以下是它的样子:

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!");
    }
}

答案 1 :(得分:0)

final TextView ergebnis =(TextView) findViewById(R.id.ergebnis);将为null,对此的任何操作都将导致NullPointerException。它应该在onCreate()

您的代码应该是:

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