Log Cat:
02-23 18:47:36.399: E/AndroidRuntime(25571): FATAL EXCEPTION: main
02-23 18:47:36.399: E/AndroidRuntime(25571): Process: com.example.calculator, PID: 25571
02-23 18:47:36.399: E/AndroidRuntime(25571): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.NullPointerException
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.os.Handler.dispatchMessage(Handler.java:102)
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.os.Looper.loop(Looper.java:136)
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-23 18:47:36.399: E/AndroidRuntime(25571): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 18:47:36.399: E/AndroidRuntime(25571): at java.lang.reflect.Method.invoke(Method.java:515)
02-23 18:47:36.399: E/AndroidRuntime(25571): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-23 18:47:36.399: E/AndroidRuntime(25571): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-23 18:47:36.399: E/AndroidRuntime(25571): at dalvik.system.NativeStart.main(Native Method)
02-23 18:47:36.399: E/AndroidRuntime(25571): Caused by: java.lang.NullPointerException
02-23 18:47:36.399: E/AndroidRuntime(25571): at com.example.calculator.MainActivity.onCreate(MainActivity.java:39)
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.Activity.performCreate(Activity.java:5231)
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-23 18:47:36.399: E/AndroidRuntime(25571): ... 11 more
02-23 18:47:48.010: D/AndroidRuntime(25647): Shutting down VM
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/screen"
android:textSize="60sp"
android:id="@+id/screen" />
<Button
android:id="@+id/num0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="@string/num0" />
<Button
android:id="@+id/num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/screen"
android:layout_marginTop="16dp"
android:text="@string/num1" />
<Button
android:id="@+id/num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/num1"
android:layout_alignBottom="@+id/num1"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/num1"
android:text="@string/num2" />
<Button
android:id="@+id/num3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/num2"
android:layout_alignBottom="@+id/num2"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/num2"
android:text="@string/num3" />
<Button
android:id="@+id/num4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/num2"
android:text="@string/num4" />
<Button
android:id="@+id/num5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/num4"
android:layout_alignBottom="@+id/num4"
android:layout_alignLeft="@+id/num2"
android:text="@string/num5" />
<Button
android:id="@+id/num6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/num5"
android:layout_alignBottom="@+id/num5"
android:layout_alignLeft="@+id/num3"
android:text="@string/num6" />
<Button
android:id="@+id/num7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/num4"
android:layout_marginTop="65dp"
android:layout_toLeftOf="@+id/num2"
android:text="@string/num7" />
<Button
android:id="@+id/num8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/num7"
android:layout_alignBottom="@+id/num7"
android:layout_alignLeft="@+id/num5"
android:text="@string/num8" />
<Button
android:id="@+id/num9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/num8"
android:layout_alignBottom="@+id/num8"
android:layout_alignLeft="@+id/num6"
android:text="@string/num9" />
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/num3"
android:layout_alignBottom="@+id/num3"
android:layout_alignParentRight="true"
android:text="@string/add" />
<Button
android:id="@+id/subtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/num9"
android:text="@string/subtract"
android:textSize="30sp" />
<Button
android:id="@+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/num7"
android:layout_alignParentRight="true"
android:text="@string/multiply" />
<Button
android:id="@+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="@string/divide" />
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/num3"
android:text="@string/clear" />
<Button
android:id="@+id/equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/num9"
android:layout_alignParentBottom="true"
android:text="@string/equal" />
</RelativeLayout>
MainActivity.java:
package com.example.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button zero, one, two, three, four, five, six, seven, eight, nine, clear, add, subtract, multiply, divide, equal;
double num1, num2, answer;
String operation;
int twoDigit;
TextView screen;
@Override
protected void onCreate(Bundle savedInstanceState) {
zero = (Button) findViewById(R.id.num0);
one = (Button) findViewById(R.id.num1);
two = (Button) findViewById(R.id.num2);
three = (Button) findViewById(R.id.num3);
four = (Button) findViewById(R.id.num4);
five = (Button) findViewById(R.id.num5);
six = (Button) findViewById(R.id.num6);
seven = (Button) findViewById(R.id.num7);
eight = (Button) findViewById(R.id.num8);
nine = (Button) findViewById(R.id.num9);
clear = (Button) findViewById(R.id.clear);
add = (Button) findViewById(R.id.add);
subtract = (Button) findViewById(R.id.subtract);
multiply = (Button) findViewById(R.id.multiply);
divide = (Button) findViewById(R.id.divide);
equal = (Button) findViewById(R.id.equal);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen.setText("0");
num1 = 0; num2 = 0;
firstNumberEntry();
}
public void firstNumberEntry() {
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = 1;
screen.setText("" + num1);
if(add.isPressed()){
operation = "add";
operations(num1, operation);
} else if(subtract.isPressed()){
operation = "subtract";
operations(num1, operation);
}else if(multiply.isPressed()){
operation = "multiply";
operations(num1, operation);
}else if(divide.isPressed()){
operation = "divide";
operations(num1, operation);
}
}
});
}
public void operations(double num1, String operation) {
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.calculator"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我是android开发的初学者,我正在尝试制作一个两位数的计算器。我还没有完成,但我想测试我已编码的内容。每当我在设备上打开应用程序时,我都会收到消息“不幸的是,计算器已停止”。我附上了我的所有代码,包括LogCat。既然我是这个东西的新手,我真的不明白什么是错的。有帮助吗?
答案 0 :(得分:4)
您没有将screen
变量设置为任何内容,因此它会初始化为null
,当您尝试调用null
的方法时会出现异常。
你应该添加
screen = (TextView) findViewById(R.id.textView);
在此声明之前。