我正在制作一个简单的应用程序,用于计算一个直到。 编辑文本输入数字时遇到问题。
在应用程序中发生的事情是用户输入收据的读数,然后输入每个单独的纸币和硬币的金额,并在点击按钮时进行计算,并为您提供新活动的结果。
应用程序立即崩溃,我相信这可能是我试图从编辑文本中获取值的方式。
我提供了我的代码和错误日志,我是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);
}
}
ResultActivity.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
-------- UPDATE ----------- 正如下面的评论所示,顺便说一句,谢谢你,这是我的主要活动,不幸的是它仍然崩溃。
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)
答案 0 :(得分:1)
在设置好布局后,您应首先引用所有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
}
答案 1 :(得分:0)
很简单。您尝试在活动显示在屏幕上之前获取值。
点击这一行
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
方法中,而不是之前。
答案 2 :(得分:0)
就像初始化了TextView resultOut
一样,你也应该初始化你正在使用的EditTexts来获取用户的输入,例如readingIn = (EditText) findViewById(R.id.your_edit_text)
。同样,在分配值时,readingIn值为null。您可以在方法中分配所有值,并可以在计算时调用该方法。
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
}
}
答案 3 :(得分:0)
你没有初始化为
readingIn = (EditText) findViewById(R.id.name_edittext);
然后你需要写在OnCreate
活动
double reading = Double.parseDouble(readingIn.getText().toString());