我正在编写这个android应用程序,但是我在logCat中遇到运行时错误: 无法启动活动:无效的双倍。
这是代码和logcat
public class CurrencyConverterActivity extends Activity {
public int to;
public int from;
public String [] val;
public String s;
public Handler handler;
public double am=0.0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
Spinner s2 = (Spinner) findViewById(R.id.spinner2);
EditText e=(EditText) findViewById(R.id.amountt);
am=Double.parseDouble(e.getText().toString());
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.name, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
val = getResources().getStringArray(R.array.value);
s1.setAdapter(adapter);
s2.setAdapter(adapter);
s1.setOnItemSelectedListener(new spinOne(1));
s2.setOnItemSelectedListener(new spinOne(2));
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView t = (TextView) findViewById(R.id.textView4);
if(from == to)
{
Toast.makeText(getApplicationContext(), "Invalid", 4000).show();
}
else
{
try {
s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
//s=getJson("http://www.google.com/ig/calculator?hl=en&q=1USD=?INR");
JSONObject jObj;
jObj = new JSONObject(s);
String exResult = jObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
double totalR=(Double.parseDouble(exResult))*am;
String r=String.valueOf(totalR);
t.setText(r);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
public String getJson(String url)throws ClientProtocolException, IOException {
StringBuilder build = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String con;
while ((con = reader.readLine()) != null) {
build.append(con);
}
return build.toString();
}
private class spinOne implements OnItemSelectedListener
{
int ide;
spinOne(int i)
{
ide =i;
}
public void onItemSelected(AdapterView<?> parent, View view,
int index, long id) {
if(ide == 1)
from = index;
else if(ide == 2)
to = index;
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}
这是LogCat:
11-07 10:02:41.294: E/AndroidRuntime(1764): FATAL EXCEPTION: main
11-07 10:02:41.294: E/AndroidRuntime(1764): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.currency.org/com.currency.org.CurrencyConverterActivity}: java.lang.NumberFormatException: Invalid double: ""
11-07 10:02:41.294: E/AndroidRuntime(1764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-07 10:02:41.294: E/AndroidRuntime(1764): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-07 10:02:41.294: E/AndroidRuntime(1764): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-07 10:02:41.294: E/AndroidRuntime(1764): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-07 10:02:41.294: E/AndroidRuntime(1764): at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 10:02:41.294: E/AndroidRuntime(1764): at android.os.Looper.loop(Looper.java:137)
11-07 10:02:41.294: E/AndroidRuntime(1764): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-07 10:02:41.294: E/AndroidRuntime(1764): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 10:02:41.294: E/AndroidRuntime(1764): at java.lang.reflect.Method.invoke(Method.java:525)
11-07 10:02:41.294: E/AndroidRuntime(1764): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-07 10:02:41.294: E/AndroidRuntime(1764): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-07 10:02:41.294: E/AndroidRuntime(1764): at dalvik.system.NativeStart.main(Native Method)
11-07 10:02:41.294: E/AndroidRuntime(1764): Caused by: java.lang.NumberFormatException: Invalid double: ""
11-07 10:02:41.294: E/AndroidRuntime(1764): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
11-07 10:02:41.294: E/AndroidRuntime(1764): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
11-07 10:02:41.294: E/AndroidRuntime(1764): at java.lang.Double.parseDouble(Double.java:295)
11-07 10:02:41.294: E/AndroidRuntime(1764): at com.currency.org.CurrencyConverterActivity.onCreate(CurrencyConverterActivity.java:52)
11-07 10:02:41.294: E/AndroidRuntime(1764): at android.app.Activity.performCreate(Activity.java:5133)
11-07 10:02:41.294: E/AndroidRuntime(1764): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-07 10:02:41.294: E/AndroidRuntime(1764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
我认为当我从editText获取文本并将其解析为double时出现错误。
但我不知道如何解决它!!
答案 0 :(得分:6)
此时您的EditText为空,""
不是有效的双精度值。你应该打电话给
am=Double.parseDouble(e.getText().toString());
输入值后输入,然后按“确定”按钮