我是Android编码的新手,在编程方面经验太少。 我想尝试制作一个计算器。我觉得它很容易但是,我被卡住了。 基本上你输入一个EditText数字,然后按" +#* /"然后输入另一个号码,然后按Enter键。但不知怎的,只要我按+或者它就崩溃了。谁能告诉我有什么问题?哦,logcat说:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.industriesoi.taschenrechner.Calc.calcPlus(Calc.java:87)
但我不知道该改变什么..
这是我的代码:
package com.industriesoi.taschenrechner;
import com.industriesoi.taschenrechner.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*
* @see SystemUiHider
*/
public class Calc extends Activity {
// INITIAL
EditText etZahl;
TextView tvErgebnis;
double zahl1=0;
double zahl2=0;
char operation = '0';
double ergebnis=0;
/**
* Whether or not the system UI should be auto-hidden after
* {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
*/
private static final boolean AUTO_HIDE = true;
/**
* If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
* user interaction before hiding the system UI.
*/
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
/**
* If set, will toggle the system UI visibility upon interaction. Otherwise,
* will show the system UI visibility upon interaction.
*/
private static final boolean TOGGLE_ON_CLICK = true;
/**
* The flags to pass to {@link SystemUiHider#getInstance}.
*/
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
/**
* The instance of the {@link SystemUiHider} for this activity.
*/
private SystemUiHider mSystemUiHider;
// ONCREATE HERE
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
etZahl = (EditText)findViewById(R.id.etZahl);
tvErgebnis = (TextView)findViewById(R.id.tvErgebnis);
setContentView(R.layout.activity_calc);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
}
public void calcPlus(View view){
String stringZahl1 = etZahl.getText().toString();
zahl1 = Double.parseDouble(stringZahl1);
operation = '+';
etZahl.setText("");
}
public void calcMinus(View view){
String stringZahl1 = etZahl.getText().toString();
zahl1 = Double.parseDouble(stringZahl1);
operation = '-';
etZahl.setText("");
}
public void calcMultiply(View view){
String stringZahl1 = etZahl.getText().toString();
zahl1 = Double.parseDouble(stringZahl1);
operation = '*';
etZahl.setText("");
}
public void calcDivide(View view){
String stringZahl1 = etZahl.getText().toString();
zahl1 = Double.parseDouble(stringZahl1);
operation = '/';
etZahl.setText("");
}
public void calcEnter(View view){
//first operate;
switch(operation){
case '+':
ergebnis=zahl1+zahl2;
break;
case '-':
ergebnis=zahl1-zahl2;
break;
case '*':
ergebnis=zahl1*zahl2;
break;
case '/':
ergebnis=zahl1/zahl2;
break;
default:
ergebnis=0;
}
//then change text;
String doubleToString = Double.toString(ergebnis);
tvErgebnis.setText(doubleToString);
}
}
_ 哦,如果你能给我一些关于编码的建议那也很好。就像我怎么能改进这个计算器,即使它没什么用呢?