Android编码错误

时间:2013-11-13 03:41:56

标签: android

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

                          }

                    }
                });
            }
}

log cat:

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)

1 个答案:

答案 0 :(得分:5)

一些可能性:

我假设您已将输入类型设置为仅数字,如果没有,请尝试,

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

XML您可以在EditText中添加android:inputType="number|decimal"

它将抛出你java.lang.NumberFormatException:,这将抛出以指示应用程序已尝试将字符串转换为其中一种数字类型,但该字符串没有适当的格式。

因此,如果您正在尝试,则无法解析或将字符串转换为double。否则语法上你的代码没问题。

如果您要强行关闭,请在try {} & catch(exception e){}内写下您的代码。这将忽略您的错误,并且不会关闭您的应用。