سؤال

I am making a simple app to use while counting a till. I am having an issue with the edit text to input the numbers.

What happens in the app is the user enters the reading of the till then the amount for each individual notes and coins and does the calculations when a button is clicked and gives you a result on a new activity.

The application crashes immediately, i believe it may be the way i am trying to get the values from the edit text.

I have supplied my code and the error log, i am new to android development so apologies if its something silly.

Thanks in advance.

TillCounter.java activity

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);

}

}

Error Log

    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----------- As advised by the comments below, thank you by the way, this is now my main activity and unfortunately it still crashes.

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());

 }

}

and gives me the error log like this -

 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)
هل كانت مفيدة؟

المحلول 2

Like you have initialised your TextView resultOut you should also initialise your EditTexts which you are using to get the inputs from the user like readingIn = (EditText) findViewById(R.id.your_edit_text).Also when you are assigning the values the readingIn value is null. You can assign all the values in a method and can invoke the method at the time of calculation .

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
 }

}

نصائح أخرى

You should reference all the EditText first after setting your Layout i.e setContentView. And write all the code after that.

Code should like

@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
}

It's Simple. You try to get values before Activity displays on the screen.

Put this lines

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;

in onCreate method,not before.

You did't initialized as

readingIn = (EditText) findViewById(R.id.name_edittext);

then you need to write inside OnCreate Activity

double reading = Double.parseDouble(readingIn.getText().toString());
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top