-1

我是 android/java 编程的初学者。我下面的程序崩溃了

currkm = (Double.parseDouble(currentkm.getText().toString()));. 我试图解决这个问题,但不能。我相信它是android中的基本编码错误。提供解决此问题的建议。

package com.example.my2app;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {

    double vehiclemileage;
    TextView mileage;
    SharedPreferences somedata;
    SharedPreferences.Editor editor;
    TextView previouskm;
    EditText currentkm;
    EditText petrollitrs;
    double currkm;
    double prevkm;
    double petrollt;
    Button resultbtn;
    String previouskmstr;
    String prevkmstring;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                setContentView(R.layout.mileagecalc);
                somedata= this.getBaseContext().getSharedPreferences("sakthivel",0);
                editor = somedata.edit();

                previouskm = (TextView)findViewById(R.id.previouskm);
                currentkm = (EditText)findViewById(R.id.currentkm);
                petrollitrs = (EditText)findViewById(R.id.petroLtr);
                mileage = (TextView)findViewById(R.id.mileage);

                previouskmstr = somedata.getString("previousmileage", null);
                previouskm.setText(previouskmstr);

                resultbtn = (Button)findViewById(R.id.button1);

                resultbtn.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View arg0) {

                          // TODO Auto-generated method stub
                          try{
                          currkm = (Double.parseDouble(currentkm.getText().toString()));
                          prevkm = (Double.parseDouble(previouskm.getText().toString()));
                          petrollt = (Double.parseDouble(petrollitrs.getText().toString()));
                          vehiclemileage = (currkm-prevkm)/petrollt;
                          prevkmstring = currentkm.getText().toString();
                          mileage.setText("your vehicle mileage is " + vehiclemileage);
                          editor.putString("previousmileage", prevkmstring);
                          editor.commit();
                          previouskm.setText(prevkmstring);
                          }
                          catch(NumberFormatException e){
                              e.printStackTrace();

                          }

                    }
                });
            }
}

日志猫:

11-13 12:52:27.671: E/JavaBinder(661): *** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
11-13 12:52:27.671: E/JavaBinder(661): java.lang.RuntimeException: android.os.DeadObjectException
11-13 12:52:27.671: E/JavaBinder(661):  at android.os.Parcel.writeException(Parcel.java:1332)
11-13 12:52:27.671: E/JavaBinder(661):  at android.os.Binder.execTransact(Binder.java:391)
4

1 回答 1

5

some of possibility:

I assume that you have set your input type as number only , if not then try it ,

edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

or from XML you can add android:inputType="number|decimal" inside your EditText.

It will throw you java.lang.NumberFormatException: and this will Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

So, you can't parse or cast string to double if you are trying that. otherwise syntactically your code is okay.

If you are getting force close then write your code inside try {} & catch(exception e){}. that will ignore your error and will not close your app.

于 2013-11-13T04:53:49.360 回答