我知道很多人都问过同样的问题,但我只是不知道发生了什么。我正在尝试在 Eclipse 中制作计算器,但我不断收到错误列表。程序注意到的文件中没有错误,虽然 layout.xml 中有错误但之前没有引起问题,所以应该不会引起问题。
07-30 08:19:50.470: D/AndroidRuntime(2071): Shutting down VM
07-30 08:19:50.470: W/dalvikvm(2071): threadid=1: thread exiting with uncaught
exception (group=0x40a421f8)
07-30 08:19:50.480: E/AndroidRuntime(2071): FATAL EXCEPTION: main
07-30 08:19:50.480: E/AndroidRuntime(2071): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{com.example.se.miun.chris.calculator/com.example.se.miun.chris.
calculator.MainActivity}: java.lang.NullPointerException
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.access$600(ActivityThread.java:123)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.os.Handler.dispatchMessage(Handler.java:99)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.os.Looper.loop(Looper.java:137)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.main(ActivityThread.java:4424)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
java.lang.reflect.Method.invokeNative(Native Method)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
java.lang.reflect.Method.invoke(Method.java:511)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
dalvik.system.NativeStart.main(Native Method)
07-30 08:19:50.480: E/AndroidRuntime(2071): Caused by: java.lang.NullPointerException
07-30 08:19:50.480: E/AndroidRuntime(2071): at
com.example.se.miun.chris.calculator.MainActivity.onCreate(MainActivity.java:60)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.Activity.performCreate(Activity.java:4465)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-30 08:19:50.480: E/AndroidRuntime(2071): ... 11 more
这是我的编码。它还没有真正做任何事情,但我只想运行它以查看它是否遇到任何错误。这是 mainActivity.java 文件。
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
Button Seven;
Button Eight;
Button Nine;
Button Four;
Button Five;
Button Six;
Button One;
Button Two;
Button Three;
Button Zero;
Button Point;
Button Negative;
TextView TextBox;
int x;
int y;
String z;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Seven = (Button)findViewById(R.id.NumberSeven);
Seven.setOnClickListener(this);
Eight = (Button)findViewById(R.id.NumberEight);
Eight.setOnClickListener(this);
Nine = (Button)findViewById(R.id.NumberNine);
Nine.setOnClickListener(this);
Four = (Button)findViewById(R.id.NumberFour);
Four.setOnClickListener(this);
Five = (Button)findViewById(R.id.NumberFive);
Five.setOnClickListener(this);
Six = (Button)findViewById(R.id.NumberSix);
Six.setOnClickListener(this);
One = (Button)findViewById(R.id.NumberOne);
One.setOnClickListener(this);
Two = (Button)findViewById(R.id.NumberTwo);
Two.setOnClickListener(this);
Three = (Button)findViewById(R.id.NumberThree);
Three.setOnClickListener(this);
Zero = (Button)findViewById(R.id.NumberZero);
Zero.setOnClickListener(this);
Point = (Button)findViewById(R.id.Point);
Point.setOnClickListener(this);
Negative = (Button)findViewById(R.id.NNegative);
Negative.setOnClickListener(this);
TextBox = (TextView)findViewById(R.id.Screen);
x = (Integer) null;
y = (Integer) null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View One) {
if(z == null){
x = 1;
TextBox.setText(x);
TextBox.setText("diggity");
}
else if(z != null) {
y = 1;
TextBox.setText(x);
TextBox.setText(z);
TextBox.setText(y);
}
}
}