Question

This is my logcat

12-16 23:29:07.309: W/dalvikvm(3821): threadid=1: thread exiting with uncaught exception (group=0x40018578)
12-16 23:29:07.309: E/AndroidRuntime(3821): FATAL EXCEPTION: main
12-16 23:29:07.309: E/AndroidRuntime(3821): java.lang.NumberFormatException: 
12-16 23:29:07.309: E/AndroidRuntime(3821):     at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
12-16 23:29:07.309: E/AndroidRuntime(3821):     at java.lang.Double.parseDouble(Double.java:318)
12-16 23:29:07.309: E/AndroidRuntime(3821):     at com.pmss.FulfillRequest$2.onClick(FulfillRequest.java:111)
12-16 23:29:07.309: E/AndroidRuntime(3821):     at android.view.View.performClick(View.java:2485)
12-16 23:29:07.309: E/AndroidRuntime(3821):     at android.view.View$PerformClick.run(View.java:9080)
12-16 23:29:07.309: E/AndroidRuntime(3821):     at android.os.Handler.handleCallback(Handler.java:587)
12-16 23:29:07.309: E/AndroidRuntime(3821):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 23:29:07.309: E/AndroidRuntime(3821):     at android.os.Looper.loop(Looper.java:130)
12-16 23:29:07.309: E/AndroidRuntime(3821):     at android.app.ActivityThread.main(ActivityThread.java:3687)
12-16 23:29:07.309: E/AndroidRuntime(3821):     at java.lang.reflect.Method.invokeNative(Native Method)
12-16 23:29:07.309: E/AndroidRuntime(3821):     at java.lang.reflect.Method.invoke(Method.java:507)
12-16 23:29:07.309: E/AndroidRuntime(3821):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-16 23:29:07.309: E/AndroidRuntime(3821):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-16 23:29:07.309: E/AndroidRuntime(3821):     at dalvik.system.NativeStart.main(Native Method)

This is my FulfillRequest.java

public class FulfillRequest extends ActionBarActivity implements OnClickListener {


    EditText parcelidtext, quantitytext, weighttext, typetext;
    Button parcelexistbutton, calculatepaymentbutton;

    JSONArray parcelconfirm = null;
    JSONArray calculate = null;
    private ProgressDialog messageDialog;
    JSONParser jsonParser = new JSONParser();

    private static final String CONFIRM_URL = "http://XXX.XXX.X.XX:1234/PMSS/trackparcel.php";
    private static final String CALCULATE_URL = "http://XXX.XXX.X.XX:1234/PMSS/shipmentcalculate.php";

    // JSON element ids from repsonse of php script:
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
    private static final String TAG_POSTS = "posts";
    private static final String TAG_PARCELID = "parcelid";
    private static final String TAG_PARCELSTATUS = "parcelstatus";
    private static final String TAG_PARCELPAYMENT = "parcelpayment";

     private double payment = 0 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fulfill_request);
        parcelidtext = (EditText) findViewById(R.id.parcelidtext);
        quantitytext = (EditText) findViewById(R.id.quantitytext);
        weighttext = (EditText) findViewById(R.id.weighttext);
        typetext = (EditText) findViewById(R.id.typetext);
        parcelexistbutton = (Button) findViewById(R.id.parcelexistbutton);
        calculatepaymentbutton = (Button) findViewById(R.id.calculatepaymentbutton);

        quantitytext.setEnabled(false);
        weighttext.setEnabled(false);
        typetext.setEnabled(false);

        parcelexistbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String ParcelID = parcelidtext.getText().toString();
                new AttemptSearch(ParcelID).execute();
            }
        });

        calculatepaymentbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String ParcelIDCal = parcelidtext.getText().toString();
                String Quantity = quantitytext.getText().toString();
                String Weight = weighttext.getText().toString();
                String Type = typetext.getText().toString();

                if(ParcelIDCal == "" || Quantity == "" || Weight == "" || Type == ""){
                    new AlertDialog.Builder(FulfillRequest.this).setTitle("Parcel Information")
                    .setMessage("Please fill in all the information first before calculate. ")
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                        }
                     })
                    .show();
                }

                else{
                    double weight = Double.parseDouble(Weight); 


                    if(weight > 0.45  || Type == "document"){
                        double baseamount = 4.50;
                        payment = ((baseamount*1.25)*1.06);                     
                    }
                    else if(weight > 0.45 || Type == "parcel"){
                        double baseamount = 5.00;
                        payment = ((baseamount*1.25)*1.06);
                    }

                    String Payment = Double.toString(payment);
                    new AttemptCalculate(ParcelIDCal,Quantity,Weight,Type,Payment).execute();
                }
            }
        });
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.fulfill_request, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:

            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    class AttemptSearch extends AsyncTask<String, String, Integer> {

        private final String TAG = null;
        boolean failure = false;
        String message;
        String ParcelID, parcelid;
        int success;

        public AttemptSearch(String ParcelID) {
            this.ParcelID = ParcelID;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            messageDialog = new ProgressDialog(FulfillRequest.this);
            messageDialog.setMessage("Attempting checking...");
            messageDialog.setIndeterminate(false);
            messageDialog.setCancelable(true);
            messageDialog.show();

        }

        protected Integer doInBackground(String... args) {

            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("parcelid", ParcelID));

                Log.d("check!", "starting");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(CONFIRM_URL, "POST",
                        params);
                // check your log for json response
                Log.d("Checking attempt", json.toString());

                // json success tag
                success =json.getInt(TAG_SUCCESS);
                message= json.getString(TAG_MESSAGE);

                if(success == 1){
                    Log.d("Retrieve Successful!", "message: " + message);
                    parcelconfirm = json.getJSONArray(TAG_POSTS);
                    JSONArray jr = parcelconfirm.getJSONArray(0);
                    JSONObject jb = jr.getJSONObject(0);
                    parcelid = jb.getString(TAG_PARCELID);
                }
                else if(success == 0){
                    Log.d("Retrieve Failed!", "message: " + message);
                }

            } catch (JSONException e) {
                Log.e(TAG, "JSON error", e);
                success = Integer.valueOf(0);
            }
            return success;

        }

        protected void onPostExecute(Integer success) {
            // dismiss the dialog once product deleted
            messageDialog.dismiss();

            if (success != null && success == 1) {
                quantitytext.setEnabled(true);
                weighttext.setEnabled(true);
                typetext.setEnabled(true);
                Toast.makeText(
                        FulfillRequest.this,
                        message == null ? "Please enter parcel ID  (success)"
                                : message, Toast.LENGTH_LONG).show();
            } 
            else {
                parcelidtext.setText(null);
                Toast.makeText(
                        FulfillRequest.this,
                        message == null ? "Please enter parcel ID  (failed)"
                                : message, Toast.LENGTH_LONG).show();
            }

        }
    }

    class AttemptCalculate extends AsyncTask<String, String, Integer> {

        private final String TAG = null;
        boolean failure = false;
        String message;
        String ParcelID, Quantity, Weight, Type, Status = "paid", Payment;
        String parcelstatus,parcelpayment;
        int success;

        public AttemptCalculate(String ParcelID, String Quantity, String Weight, String Type, String Payment) {
            this.ParcelID = ParcelID;
            this.Quantity = Quantity;
            this.Weight = Weight;
            this.Type = Type;
            this.Payment = Payment;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            messageDialog = new ProgressDialog(FulfillRequest.this);
            messageDialog.setMessage("Attempting calculating...");
            messageDialog.setIndeterminate(false);
            messageDialog.setCancelable(true);
            messageDialog.show();

        }

        protected Integer doInBackground(String... args) {

            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("parcelid", ParcelID));
                params.add(new BasicNameValuePair("parcelquantity", Quantity));
                params.add(new BasicNameValuePair("parcelweight", Weight));
                params.add(new BasicNameValuePair("parceltype", Type));
                params.add(new BasicNameValuePair("parcelpayment", Payment));
                params.add(new BasicNameValuePair("parcelstatus", Status));

                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(CALCULATE_URL, "POST",
                        params);
                // check your log for json response
                Log.d("Tracking attempt", json.toString());

                // json success tag
                success =json.getInt(TAG_SUCCESS);
                message= json.getString(TAG_MESSAGE);

                if(success == 1 ){
                    Log.d("Retrieve Successful!", "message: " + message);
                    calculate = json.getJSONArray(TAG_POSTS);
                    JSONArray jr = calculate.getJSONArray(0);
                    JSONObject jb = jr.getJSONObject(0);
                    parcelpayment = jb.getString(TAG_PARCELPAYMENT);
                    parcelstatus = jb.getString(TAG_PARCELSTATUS);
                }
                else if(success == 0){
                    Log.d("Retrieve Failed!", "message: " + message);
                }

            } catch (JSONException e) {
                Log.e(TAG, "JSON error", e);
                success = Integer.valueOf(0);
            }
            return success;

        }

        protected void onPostExecute(Integer success) {
            // dismiss the dialog once product deleted
            messageDialog.dismiss();
            if (success != null && success == 1) {              
                    new AlertDialog.Builder(FulfillRequest.this).setTitle("Parcel Information")
                    .setMessage(" Your Parcel Status is " + parcelstatus + ". Your Payment Amount is RM" + parcelpayment + ". ")
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            parcelidtext.setText(null);
                            quantitytext.setText(null);
                            quantitytext.setEnabled(false);                         
                            weighttext.setText(null);
                            weighttext.setEnabled(false);
                            typetext.setText(null);                                                     
                            typetext.setEnabled(false);
                        }
                     })
                    .show();
                Toast.makeText(
                        FulfillRequest.this,
                        message == null ? "Please enter parcel ID  (success)"
                                : message, Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(
                        FulfillRequest.this,
                        message == null ? "Please enter parcel ID  (failed)"
                                : message, Toast.LENGTH_LONG).show();
            }

        }
    }
}

Why does this error message occur to me? What should I do? I think that maybe is because of my double variable of weight.

line 111 is double weight = Double.parseDouble(Weight); in this method calculatepaymentbutton.setOnClickListener(new View.OnClickListener()

I wonder there is a way to eliminate this problem? Cause what I want to do is that when I click calculatepaymentbutton, I want my system to check that all EditText is all filled or not, but when I click it this error logcat occur~

Was it helpful?

Solution

These comparisons are part of the problem:

if (ParcelIDCal == "" || Quantity == "" || Weight == "" || Type == "")

They should be:

if (ParcelIDCal == null || ParcelIDCal.trim().equals("") ||
    Quantity == null || Quantity.trim().equals("") ||
    Weight == null || Weight.trim().equals("") ||
    Type == null || Type.trim().equals(""))

And all the parts where you're comparing strings like this are wrong:

Type == "document"

That's not how you compare Strings for equality in Java, you must use:

Type.equals("document")

Or even better, to take into consideration the possibility that the variable is null it's better to do this:

"document".equals(Type)

And by the way - variables and attributes in Java should start with a lowercase letter, it's the convention.

OTHER TIPS

If you want to campare Strings, use:

"".equals(ParcelIDCal)

or even better, to check if string is empty or blank:

StringUtils.isNotBlank(ParcelIDCal)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top