我正在使用SOAP Web服务在Android中尝试货币转换器。一切正常。但是,当我点击转换按钮时,应用程序崩溃并出现错误。 你能救我一下......
12-31 10:56:21.508: E/WindowManager(4789): Activity com.soapwebservice.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40e11d98 V.E..... R.....ID 0,0-320,162} that was originally added here
12-31 10:56:21.508: E/WindowManager(4789): android.view.WindowLeaked: Activity com.soapwebservice.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40e11d98 V.E..... R.....ID 0,0-320,162} that was originally added here
12-31 10:56:21.508: E/WindowManager(4789): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
12-31 10:56:21.508: E/WindowManager(4789): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
12-31 10:56:21.508: E/WindowManager(4789): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
12-31 10:56:21.508: E/WindowManager(4789): at android.app.Dialog.show(Dialog.java:281)
12-31 10:56:21.508: E/WindowManager(4789): at com.soapwebservice.MainActivity$DownloadData.onPreExecute(MainActivity.java:112)
12-31 10:56:21.508: E/WindowManager(4789): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
12-31 10:56:21.508: E/WindowManager(4789): at android.os.AsyncTask.execute(AsyncTask.java:534)
12-31 10:56:21.508: E/WindowManager(4789): at com.soapwebservice.MainActivity$3.onClick(MainActivity.java:93)
12-31 10:56:21.508: E/WindowManager(4789): at android.view.View.performClick(View.java:4204)
12-31 10:56:21.508: E/WindowManager(4789): at android.view.View$PerformClick.run(View.java:17355)
12-31 10:56:21.508: E/WindowManager(4789): at android.os.Handler.handleCallback(Handler.java:725)
12-31 10:56:21.508: E/WindowManager(4789): at android.os.Handler.dispatchMessage(Handler.java:92)
12-31 10:56:21.508: E/WindowManager(4789): at android.os.Looper.loop(Looper.java:137)
12-31 10:56:21.508: E/WindowManager(4789): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-31 10:56:21.508: E/WindowManager(4789): at java.lang.reflect.Method.invokeNative(Native Method)
12-31 10:56:21.508: E/WindowManager(4789): at java.lang.reflect.Method.invoke(Method.java:511)
12-31 10:56:21.508: E/WindowManager(4789): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-31 10:56:21.508: E/WindowManager(4789): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-31 10:56:21.508: E/WindowManager(4789): at dalvik.system.NativeStart.main(Native Method)
CODE
String[] country = { "British", "European", "American", "Indian" };
String[] currency = { "Pounds ", "Euro", "Dollor", "Rupee" };
String[] currency_abr = { "GBP", "EUR", "USD", "INR" };
String to_curr, from_curr;
int arr_images[] = { R.drawable.pound, R.drawable.euro, R.drawable.dollor,
R.drawable.rupee };
TextView converted;
Spinner from_currency;
Spinner to_currency;
EditText input;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
to_curr = "INR";
from_curr = "USD";
setContentView(R.layout.main);
converted = (TextView) findViewById(R.id.textView2);
input = (EditText) findViewById(R.id.editText1);
converted.setText("");
from_currency = (Spinner) findViewById(R.id.spinner1);
from_currency.setAdapter(new MyAdapter(MainActivity.this,
R.layout.row, country));
from_currency.setSelection(2);
to_currency = (Spinner) findViewById(R.id.spinner2);
to_currency.setAdapter(new MyAdapter(MainActivity.this,
R.layout.row, country));
to_currency.setSelection(3);
from_currency.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View v,
int position, long id) {
Toast.makeText(getApplicationContext(),
"" + currency_abr[position], Toast.LENGTH_LONG).show();
converted.setHint(currency[position]);
from_curr = currency_abr[position];
}
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
to_currency.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View v,
int position, long id) {
Toast.makeText(getApplicationContext(),
"" + currency_abr[position], Toast.LENGTH_LONG).show();
converted.setHint(currency[position]);
to_curr = currency_abr[position];
}
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DownloadData().execute(to_curr, from_curr, input.getText()
.toString());
}
});
converted = (TextView) findViewById(R.id.textView2);
}
class DownloadData extends AsyncTask<String, String, String> {
ProgressDialog pd = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setTitle("Converting...");
pd.setMessage("Please wait...");
pd.setCancelable(false);
pd.show();
}
@Override
protected String doInBackground(String... params) {
String s = "";
String theResult = "";
String to_curr = params[0];
String from_curr = params[1];
String value = params[2];
try {
s = new SOAPclass().remotereq(to_curr, from_curr, value);
}
catch (Exception e) {
e.printStackTrace();
}
theResult = s;
return theResult;
}
@Override
protected void onPostExecute(String theResult) {
super.onPostExecute(theResult);
pd.dismiss();
double x;
try {
x = Double.parseDouble(theResult);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
x = 1;
input.setText("");
}
converted.setText(new Double(x).toString());
}
}
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
TextView label = (TextView) row.findViewById(R.id.country);
label.setText(country[position]);
TextView sub = (TextView) row.findViewById(R.id.currency);
sub.setText(currency[position]);
ImageView icon = (ImageView) row.findViewById(R.id.image);
icon.setImageResource(arr_images[position]);
return row;
}
}