Debuger
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.otel.stupidbuttons, PID: 5599
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.otel.stupidbuttons/com.example.otel.stupidbuttons.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:3132)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
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:117)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:56)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:200)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.otel.stupidbuttons.MainActivity.<init>(MainActivity.java:10)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1096)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3122)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
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)
这是我的Java文件:
package com.example.otel.stupidbuttons;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button b1g1 = findViewById(R.id.b1g1);
Button b2g1 = findViewById(R.id.b2g1);
Button b3g1 = findViewById(R.id.b3g1);
Button b1g2 = findViewById(R.id.b1g2);
Button b2g2 = findViewById(R.id.b2g2);
Button b3g2 = findViewById(R.id.b3g2);
TextView leftText = findViewById(R.id.leftText);
TextView rightText = findViewById(R.id.rightText);
int leftValue = 0;
int rightValue = 0;
public void display1 (int a){
leftText.setText(a);
}
public void display2 (int a){
rightText.setText(a);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1g1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = leftValue;
a = a+3;
leftValue = a;
display1(a);
}
});
b2g1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = leftValue;
a = a+2;
leftValue = a;
display1(a);
}
});
b3g1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = leftValue;
a = a+1;
leftValue = a;
display1(a);
}
});
b1g2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = rightValue;
a = a+3;
rightValue = a;
display2(a);
}
});
b2g2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = rightValue;
a = a+2;
rightValue = a;
display2(a);
}
});
b3g2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = rightValue;
a = a+1;
rightValue = a;
display2(a);
}
});
}
}
我正在尝试为TextView分配值。当用户按下按钮时,它会增加左侧或右侧的值,具体取决于按下按钮的哪一侧。能帮助我,我是这门语言的新手。