Java 使用编辑文本进行计算
我正在制作一个简单的应用程序,在计算钱柜时使用。 我对输入数字的编辑文本有问题 应用程序中发生的事情是,用户输入每个纸币和硬币的读数,然后在单击按钮时进行计算,并给出新活动的结果 应用程序立即崩溃,我相信这可能是我试图从编辑文本中获取值的方式 我已经提供了我的代码和错误日志,我是android开发的新手,所以如果这是一件愚蠢的事情,我深表歉意 提前谢谢 TillCounter.java活动Java 使用编辑文本进行计算,java,android,android-activity,android-edittext,error-log,Java,Android,Android Activity,Android Edittext,Error Log,我正在制作一个简单的应用程序,在计算钱柜时使用。 我对输入数字的编辑文本有问题 应用程序中发生的事情是,用户输入每个纸币和硬币的读数,然后在单击按钮时进行计算,并给出新活动的结果 应用程序立即崩溃,我相信这可能是我试图从编辑文本中获取值的方式 我已经提供了我的代码和错误日志,我是android开发的新手,所以如果这是一件愚蠢的事情,我深表歉意 提前谢谢 TillCounter.java活动 package com.example.tillcounter; import android.os.B
package com.example.tillcounter;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class TillCounter extends Activity implements OnClickListener{
Button calcButton;
EditText readingIn, floatIn, noteIn, fiftiesIn, twentiesIn, tensIn, fivesIn, twosIn, onesIn, fiftyCentsIn, twentyCentsIn, tenCentsIn;
TextView resultOut;
//int reading = new Integer(readingIn.intValue());
double reading = Double.parseDouble(readingIn.getText().toString());
double tillFloat = Double.parseDouble(floatIn.getText().toString());
double largeNotes = Double.parseDouble(noteIn.getText().toString());
double fifties = Double.parseDouble(fiftiesIn.getText().toString());
double twenties = Double.parseDouble(twentiesIn.getText().toString());
double tens = Double.parseDouble(tensIn.getText().toString());
double fives = Double.parseDouble(fivesIn.getText().toString());
double twos = Double.parseDouble(twosIn.getText().toString());
double ones = Double.parseDouble(onesIn.getText().toString());
double fiftyC = Double.parseDouble(fiftyCentsIn.getText().toString());
double twentyC = Double.parseDouble(twentyCentsIn.getText().toString());
double tenC = Double.parseDouble(tenCentsIn.getText().toString());
double result = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_till_counter);
resultOut = (TextView) findViewById(R.id.textView1);
Button calcButton = (Button) findViewById(R.id.calcButton);
calcButton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.till_counter, menu);
return true;
}
@Override
public void onClick(View v) {
Log.i("clicks","You Clicked calculate");
result = (reading - tillFloat - largeNotes - fifties - twenties - tens - fives - twos - ones - fiftyC - twentyC - tenC);
if(result > 0){
resultOut.setText("The till is up by: " + result);
}else if(result < 0){
resultOut.setText("The till is down by: " + result);
}else{
resultOut.setText("The till is even at: " + reading);
}
Intent i=new Intent(TillCounter.this, ResultActivity.class);
startActivity(i);
}
}
错误日志
09-03 11:56:58.033: E/Trace(19124): error opening trace file: No such file or directory (2)
09-03 11:56:58.063: W/dalvikvm(19124): threadid=1: thread exiting with uncaught exception (group=0x40d59378)
09-03 11:56:58.073: E/AndroidRuntime(19124): FATAL EXCEPTION: main
09-03 11:56:58.073: E/AndroidRuntime(19124): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tillcounter/com.example.tillcounter.TillCounter}: java.lang.NullPointerException
09-03 11:56:58.073: E/AndroidRuntime(19124): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1995)
09-03 11:56:58.073: E/AndroidRuntime(19124): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
09-03 11:56:58.073: E/AndroidRuntime(19124): at android.app.ActivityThread.access$600(ActivityThread.java:138)
09-03 11:56:58.073: E/AndroidRuntime(19124): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
09-03 11:56:58.073: E/AndroidRuntime(19124): at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 11:56:58.073: E/AndroidRuntime(19124): at android.os.Looper.loop(Looper.java:213)
09-03 11:56:58.073: E/AndroidRuntime(19124): at android.app.ActivityThread.main(ActivityThread.java:4787)
09-03 11:56:58.073: E/AndroidRuntime(19124): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 11:56:58.073: E/AndroidRuntime(19124): at java.lang.reflect.Method.invoke(Method.java:511)
09-03 11:56:58.073: E/AndroidRuntime(19124): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
09-03 11:56:58.073: E/AndroidRuntime(19124): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
09-03 11:56:58.073: E/AndroidRuntime(19124): at dalvik.system.NativeStart.main(Native Method)
09-03 11:56:58.073: E/AndroidRuntime(19124): Caused by: java.lang.NullPointerException
09-03 11:56:58.073: E/AndroidRuntime(19124): at com.example.tillcounter.TillCounter.<init>(TillCounter.java:22)
09-03 11:56:58.073: E/AndroidRuntime(19124): at java.lang.Class.newInstanceImpl(Native Method)
09-03 11:56:58.073: E/AndroidRuntime(19124): at java.lang.Class.newInstance(Class.java:1319)
09-03 11:56:58.073: E/AndroidRuntime(19124): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
09-03 11:56:58.073: E/AndroidRuntime(19124): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1986)
09-03 11:56:58.073: E/AndroidRuntime(19124): ... 11 more
很简单。您尝试在屏幕上显示活动之前获取值 把这句话写下来
double reading = Double.parseDouble(readingIn.getText().toString());
double tillFloat = Double.parseDouble(floatIn.getText().toString());
double largeNotes = Double.parseDouble(noteIn.getText().toString());
double fifties = Double.parseDouble(fiftiesIn.getText().toString());
double twenties = Double.parseDouble(twentiesIn.getText().toString());
double tens = Double.parseDouble(tensIn.getText().toString());
double fives = Double.parseDouble(fivesIn.getText().toString());
double twos = Double.parseDouble(twosIn.getText().toString());
double ones = Double.parseDouble(onesIn.getText().toString());
double fiftyC = Double.parseDouble(fiftyCentsIn.getText().toString());
double twentyC = Double.parseDouble(twentyCentsIn.getText().toString());
double tenC = Double.parseDouble(tenCentsIn.getText().toString());
double result = 0;
在
onCreate
方法中,而不是在之前。您应该在设置布局后首先引用所有EditText
,即setContentView
。
然后写下所有的代码
代码应该是
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_till_counter);
readingIn = (EditText) findViewById(R.id.readingIn);
//and so on for floatIn, noteIn, fiftiesIn etc..
//int reading = new Integer(readingIn.intValue());
reading = Double.parseDouble(readingIn.getText().toString());
tillFloat = Double.parseDouble(floatIn.getText().toString());
largeNotes = Double.parseDouble(noteIn.getText().toString());
fifties = Double.parseDouble(fiftiesIn.getText().toString());
twenties = Double.parseDouble(twentiesIn.getText().toString());
tens = Double.parseDouble(tensIn.getText().toString());
fives = Double.parseDouble(fivesIn.getText().toString());
twos = Double.parseDouble(twosIn.getText().toString());
ones = Double.parseDouble(onesIn.getText().toString());
fiftyC = Double.parseDouble(fiftyCentsIn.getText().toString());
twentyC = Double.parseDouble(twentyCentsIn.getText().toString());
tenC = Double.parseDouble(tenCentsIn.getText().toString());
//other code
}
就像你已经初始化了你的
TextView结果一样
你也应该初始化你的EditText,你用它来从用户那里获取输入,比如readingIn=(EditText)findViewById(R.id.your_edit_text)
。另外,当你赋值时,readingIn值是空的。您可以指定方法中的所有值,并可以在计算时调用该方法
package com.example.tillcounter;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class TillCounter extends Activity implements OnClickListener{
Button calcButton;
EditText readingIn, floatIn, noteIn, fiftiesIn, twentiesIn, tensIn, fivesIn, twosIn,....;
TextView resultOut;
double reading, tillFloat, largeNotes, fifties ,…..;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_till_counter);
resultOut = (TextView) findViewById(R.id.textView1);
readingIn = (TextView) findViewById(R.id. readingIn);
floatIn = (TextView) findViewById(R.id. floatIn);
noteIn = (TextView) findViewById(R.id. noteIn);
twentiesIn = (TextView) findViewById(R.id. twentiesIn);
///So on
Button calcButton = (Button) findViewById(R.id.calcButton);
calcButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Log.i("clicks","You Clicked calculate");
calculate();
result = (reading - tillFloat - largeNotes - fifties - twenties - tens -
fives - twos - ones - fiftyC - twentyC - tenC);
if(result > 0){
resultOut.setText("The till is up by: " + result);
}else if(result < 0){
resultOut.setText("The till is down by: " + result);
}else{
resultOut.setText("The till is even at: " + reading);
}
Intent i=new Intent(TillCounter.this, ResultActivity.class);
startActivity(i);
}
public void calculate(){
reading = Double.parseDouble(readingIn.getText().toString());
tillFloat = Double.parseDouble(floatIn.getText().toString());
largeNotes = Double.parseDouble(noteIn.getText().toString());
fifties = Double.parseDouble(fiftiesIn.getText().toString());
twenties = Double.parseDouble(twentiesIn.getText().toString());
////so on
}
}
package com.example.tillcounter;
导入android.os.Bundle;
导入android.app.Activity;
导入android.content.Intent;
导入android.util.Log;
导入android.view.Menu;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
公共类TillCounter扩展活动实现OnClickListener{
按钮calcButton;
编辑文本读入,浮动,注释,五十辛,二十辛,时态,五辛,二辛,。。。。;
文本视图结果;
双读,tillFloat,largeNotes,五十年代,…。。;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u-till\u计数器);
resultOut=(TextView)findViewById(R.id.textView1);
readingIn=(TextView)findViewById(R.id.readingIn);
floatIn=(TextView)findViewById(R.id.floatIn);
noteIn=(TextView)findViewById(R.id.noteIn);
twentiesIn=(TextView)findViewById(R.id.twentiesIn);
///诸如此类
按钮calcButton=(按钮)findViewById(R.id.calcButton);
setOnClickListener(this);
}
@凌驾
公共void onClick(视图v){
Log.i(“点击”,“你点击了计算”);
计算();
结果=(读数-tillFloat-largeNotes-五十-二十-十-
五,二,一,五,二十,十;;
如果(结果>0){
resultOut.setText(“钱柜由:“+result”打开);
}否则如果(结果<0){
resultOut.setText(“收银机下降了:“+result”);
}否则{
resultOut.setText(“收银机在:+读数处为偶数”);
}
意图i=新意图(TillCounter.this、ResultActivity.class);
星触觉(i);
}
公共空间计算(){
reading=Double.parseDouble(readingIn.getText().toString());
tillFloat=Double.parseDouble(floatIn.getText().toString());
largeNotes=Double.parseDouble(noteIn.getText().toString());
五十个=Double.parseDouble(五十个sin.getText().toString());
twenties=Double.parseDouble(twentiesIn.getText().toString());
////诸如此类
}
}
您没有初始化为
readingIn = (EditText) findViewById(R.id.name_edittext);
然后,您需要在创建活动中写入OnCreate
Activity
double reading = Double.parseDouble(readingIn.getText().toString());
你是如何获得信息的?您的EditText代码在哪里?通过编辑文本,我试图将其存储在一个变量中,并使用该变量进行计算。下面有很多相同的解决方案,请选择任何解决方案。如果有帮助,您应该接受任何答案:)
package com.example.tillcounter;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class TillCounter extends Activity implements OnClickListener{
Button calcButton;
EditText readingIn, floatIn, noteIn, fiftiesIn, twentiesIn, tensIn, fivesIn, twosIn,....;
TextView resultOut;
double reading, tillFloat, largeNotes, fifties ,…..;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_till_counter);
resultOut = (TextView) findViewById(R.id.textView1);
readingIn = (TextView) findViewById(R.id. readingIn);
floatIn = (TextView) findViewById(R.id. floatIn);
noteIn = (TextView) findViewById(R.id. noteIn);
twentiesIn = (TextView) findViewById(R.id. twentiesIn);
///So on
Button calcButton = (Button) findViewById(R.id.calcButton);
calcButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Log.i("clicks","You Clicked calculate");
calculate();
result = (reading - tillFloat - largeNotes - fifties - twenties - tens -
fives - twos - ones - fiftyC - twentyC - tenC);
if(result > 0){
resultOut.setText("The till is up by: " + result);
}else if(result < 0){
resultOut.setText("The till is down by: " + result);
}else{
resultOut.setText("The till is even at: " + reading);
}
Intent i=new Intent(TillCounter.this, ResultActivity.class);
startActivity(i);
}
public void calculate(){
reading = Double.parseDouble(readingIn.getText().toString());
tillFloat = Double.parseDouble(floatIn.getText().toString());
largeNotes = Double.parseDouble(noteIn.getText().toString());
fifties = Double.parseDouble(fiftiesIn.getText().toString());
twenties = Double.parseDouble(twentiesIn.getText().toString());
////so on
}
}
readingIn = (EditText) findViewById(R.id.name_edittext);
double reading = Double.parseDouble(readingIn.getText().toString());