[英]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.