我正在制作一个简单的应用程序,以便在计数时使用。我在编辑文本以输入数字时遇到问题。
在应用程序中发生的情况是,用户输入收银台的读数,然后输入每个单独的纸币和硬币的金额,并在单击按钮时进行计算,并为您提供新活动的结果。
应用程序立即崩溃,我相信这可能是我试图从编辑文本中获取值的方式。
我已经提供了我的代码和错误日志,我是 android 开发的新手,如果它有些愚蠢,我深表歉意。
提前致谢。
TillCounter.java 活动
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);
}
}
结果活动.java
package com.example.tillcounter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ResultActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_result);
}
}
错误日志
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
--------更新----------- 正如下面的评论所建议的,顺便谢谢你,这现在是我的主要活动,不幸的是它仍然崩溃。
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;
double reading, tillFloat, largeNotes, fifties, twenties, tens, fives, twos, ones, fiftyC, twentyC, tenC;
//int reading = new Integer(readingIn.intValue());
double result = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_till_counter);
resultOut = (TextView) findViewById(R.id.results);
readingIn = (EditText) findViewById(R.id.readingIn);
floatIn = (EditText) findViewById(R.id.floatIn);
noteIn = (EditText) findViewById(R.id.notesIn);
fiftiesIn = (EditText) findViewById(R.id.fiftiesIn);
twentiesIn = (EditText) findViewById(R.id.twentiesIn);
tensIn = (EditText) findViewById(R.id.tensIn);
fivesIn = (EditText) findViewById(R.id.fivesIn);
twosIn = (EditText) findViewById(R.id.twosIn);
onesIn = (EditText) findViewById(R.id.onesIn);
fiftyCentsIn = (EditText) findViewById(R.id.fiftyCentIn);
twentyCentsIn = (EditText) findViewById(R.id.twentyCentIn);
tenCentsIn = (EditText) findViewById(R.id.tenCentIn);
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");
//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());
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());
}
}
并给我这样的错误日志-
09-03 15:20:55.083: E/Trace(6712): error opening trace file: No such file or directory (2)
09-03 15:21:14.753: W/dalvikvm(6712): threadid=1: thread exiting with uncaught exception (group=0x40d59378)
09-03 15:21:14.763: E/AndroidRuntime(6712): FATAL EXCEPTION: main
09-03 15:21:14.763: E/AndroidRuntime(6712): java.lang.NullPointerException
09-03 15:21:14.763: E/AndroidRuntime(6712): at com.example.tillcounter.TillCounter.onClick(TillCounter.java:68)
09-03 15:21:14.763: E/AndroidRuntime(6712): at android.view.View.performClick(View.java:4147)
09-03 15:21:14.763: E/AndroidRuntime(6712): at android.view.View$PerformClick.run(View.java:17161)
09-03 15:21:14.763: E/AndroidRuntime(6712): at android.os.Handler.handleCallback(Handler.java:615)
09-03 15:21:14.763: E/AndroidRuntime(6712): at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 15:21:14.763: E/AndroidRuntime(6712): at android.os.Looper.loop(Looper.java:213)
09-03 15:21:14.763: E/AndroidRuntime(6712): at android.app.ActivityThread.main(ActivityThread.java:4787)
09-03 15:21:14.763: E/AndroidRuntime(6712): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 15:21:14.763: E/AndroidRuntime(6712): at java.lang.reflect.Method.invoke(Method.java:511)
09-03 15:21:14.763: E/AndroidRuntime(6712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
09-03 15:21:14.763: E/AndroidRuntime(6712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
09-03 15:21:14.763: E/AndroidRuntime(6712): at dalvik.system.NativeStart.main(Native Method)