我正在做一个货币转换器但是当我在editText上放一个值并点击botton转换没有发生任何事情时,我已经看到代码多重时间但没有看到错误,任何人都可以帮助我。 我是android的初学者
public class MainActivity extends Activity implements View.OnClickListener{
int money1, money2,money3,money4;
int[] money = {money1,money2,money3,money4};
int from;
TextView rate;
Button button1;
EditText ed1;
EditText ed2;
EditText ed3;
EditText ed4;
Spinner spinner1;
Spinner spinner2;
Spinner spinner3;
Spinner spinner4;
EditText[] values = {ed1,ed2,ed3,ed4};
String[] spinnerValues = { "ANGOLAN KWANZA" , "ARGENTINE PESO" , "ARMENIAN DRAM" , "ARUBAN FLORIN" , "AUSTRALIAN DOLLAR" , "BELARUSIAN RUBLE" , "BOLIVIAN BOLIVIANO" , "BRAZILIAN REAL" , "CAMBODIAN RIEL","CANADIAN DOLLAR","CHINESE YUAN RENMINBI","EURO","INDIAN RUPEE","JAPANESE YEN", "LIBIAN DINAR","SWISS FRANC","THAI BAHT","BRITISH POUND","US DOLLAR","VIETNAMESE DONG"};
String[] simbol = { "AOA", "ARS", "AMD","AWG","AUD","BYR","BOB","BRL","KHR","CAD","CNY","EUR",
"INR","JPY","CHF","THB","GBP","USD","VND","LYD"};
int arr_images[] = { R.drawable.angola, R.drawable.argentina,
R.drawable.armenia, R.drawable.aruba, R.drawable.australia,
R.drawable.bielorussia, R.drawable.bolivia, R.drawable.brasil,
R.drawable.cambodia, R.drawable.canada, R.drawable.china,
R.drawable.eur, R.drawable.india, R.drawable.japan,
R.drawable.libya, R.drawable.swiss, R.drawable.thai, R.drawable.uk,
R.drawable.us, R.drawable.vietnam };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
ed3 = (EditText) findViewById(R.id.editText3);
ed4 = (EditText) findViewById(R.id.editText4);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner3 = (Spinner) findViewById(R.id.spinner3);
spinner4 = (Spinner) findViewById(R.id.spinner4);
Spinner mySpinner = (Spinner)
findViewById(R.id.spinner1);
mySpinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner, spinnerValues));
Spinner mySpinner1 = (Spinner)
findViewById(R.id.spinner2);
mySpinner1.setAdapter(new MyAdapter(this, R.layout.custom_spinner, spinnerValues));
Spinner mySpinner2 = (Spinner)
findViewById(R.id.spinner3);
mySpinner2.setAdapter(new MyAdapter(this, R.layout.custom_spinner, spinnerValues));
Spinner mySpinner3 = (Spinner)
findViewById(R.id.spinner4);
mySpinner3.setAdapter(new MyAdapter(this, R.layout.custom_spinner, spinnerValues));
button1.setOnClickListener(this);
}
public class MyAdapter extends ArrayAdapter<String>
{ public MyAdapter(Context ctx, int txtViewResourceId, String[] objects)
{ super(ctx, txtViewResourceId, objects); }
@Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt)
{ return getCustomView(position, cnvtView, prnt); }
@Override
public View getView(int pos, View cnvtView, ViewGroup prnt)
{ return getCustomView(pos, cnvtView, prnt); }
public View getCustomView(int position, View convertView, ViewGroup parent)
{ LayoutInflater inflater = getLayoutInflater();
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);
String[] spinnerValues = { "ANGOLAN KWANZA" , "ARGENTINE PESO" , "ARMENIAN DRAM" , "ARUBAN FLORIN" , "AUSTRALIAN DOLLAR" , "BELARUSIAN RUBLE" , "BOLIVIAN BOLIVIANO" , "BRAZILIAN REAL" , "CAMBODIAN RIEL","CANADIAN DOLLAR","CHINESE YUAN RENMINBI","EURO","INDIAN RUPEE","JAPANESE YEN", "LIBIAN DINAR","SWISS FRANC","THAI BAHT","BRITISH POUND","US DOLLAR","VIETNAMESE DONG"};
int arr_images[] = { R.drawable.angola, R.drawable.argentina,
R.drawable.armenia, R.drawable.aruba, R.drawable.australia,
R.drawable.bielorussia, R.drawable.bolivia, R.drawable.brasil,
R.drawable.cambodia, R.drawable.canada, R.drawable.china,
R.drawable.eur, R.drawable.india, R.drawable.japan,
R.drawable.libya, R.drawable.swiss, R.drawable.thai, R.drawable.uk,
R.drawable.us, R.drawable.vietnam };
TextView main_text = (TextView) mySpinner .findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
ImageView left_icon = (ImageView) mySpinner .findViewById(R.id.left_pic);
left_icon.setImageResource(arr_images[position]);
return mySpinner;
}
public View getCustomView2(int position, View convertView, ViewGroup parent)
{ LayoutInflater inflater = getLayoutInflater();
View mySpinner1 = inflater.inflate(R.layout.custom_spinner, parent, false);
String[] spinnerValues = { "ANGOLAN KWANZA" , "ARGENTINE PESO" , "ARMENIAN DRAM" , "ARUBAN FLORIN" , "AUSTRALIAN DOLLAR" , "BELARUSIAN RUBLE" , "BOLIVIAN BOLIVIANO" , "BRAZILIAN REAL" , "CAMBODIAN RIEL","CANADIAN DOLLAR","CHINESE YUAN RENMINBI","EURO","INDIAN RUPEE","JAPANESE YEN", "LIBIAN DINAR","SWISS FRANC","THAI BAHT","BRITISH POUND","US DOLLAR","VIETNAMESE DONG"};
int arr_images[] = { R.drawable.angola, R.drawable.argentina,
R.drawable.armenia, R.drawable.aruba, R.drawable.australia,
R.drawable.bielorussia, R.drawable.bolivia, R.drawable.brasil,
R.drawable.cambodia, R.drawable.canada, R.drawable.china,
R.drawable.eur, R.drawable.india, R.drawable.japan,
R.drawable.libya, R.drawable.swiss, R.drawable.thai, R.drawable.uk,
R.drawable.us, R.drawable.vietnam };
TextView main_text = (TextView) mySpinner1 .findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
ImageView left_icon = (ImageView) mySpinner1 .findViewById(R.id.left_pic);
left_icon.setImageResource(arr_images[position]);
return mySpinner1;}
public View getCustomView3(int position, View convertView, ViewGroup parent)
{ LayoutInflater inflater = getLayoutInflater();
View mySpinner2 = inflater.inflate(R.layout.custom_spinner, parent, false);
String[] spinnerValues = { "ANGOLAN KWANZA" , "ARGENTINE PESO" , "ARMENIAN DRAM" , "ARUBAN FLORIN" , "AUSTRALIAN DOLLAR" , "BELARUSIAN RUBLE" , "BOLIVIAN BOLIVIANO" , "BRAZILIAN REAL" , "CAMBODIAN RIEL","CANADIAN DOLLAR","CHINESE YUAN RENMINBI","EURO","INDIAN RUPEE","JAPANESE YEN", "LIBIAN DINAR","SWISS FRANC","THAI BAHT","BRITISH POUND","US DOLLAR","VIETNAMESE DONG"};
int arr_images[] = { R.drawable.angola, R.drawable.argentina,
R.drawable.armenia, R.drawable.aruba, R.drawable.australia,
R.drawable.bielorussia, R.drawable.bolivia, R.drawable.brasil,
R.drawable.cambodia, R.drawable.canada, R.drawable.china,
R.drawable.eur, R.drawable.india, R.drawable.japan,
R.drawable.libya, R.drawable.swiss, R.drawable.thai, R.drawable.uk,
R.drawable.us, R.drawable.vietnam };
TextView main_text = (TextView) mySpinner2 .findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
ImageView left_icon = (ImageView) mySpinner2 .findViewById(R.id.left_pic);
left_icon.setImageResource(arr_images[position]);
return mySpinner2;}
public View getCustomView4(int position, View convertView, ViewGroup parent)
{ LayoutInflater inflater = getLayoutInflater();
View mySpinner3 = inflater.inflate(R.layout.custom_spinner, parent, false);
String[] spinnerValues = { "ANGOLAN KWANZA" , "ARGENTINE PESO" , "ARMENIAN DRAM" , "ARUBAN FLORIN" , "AUSTRALIAN DOLLAR" , "BELARUSIAN RUBLE" , "BOLIVIAN BOLIVIANO" , "BRAZILIAN REAL" , "CAMBODIAN RIEL","CANADIAN DOLLAR","CHINESE YUAN RENMINBI","EURO","INDIAN RUPEE","JAPANESE YEN", "LIBIAN DINAR","SWISS FRANC","THAI BAHT","BRITISH POUND","US DOLLAR","VIETNAMESE DONG"};
int arr_images[] = { R.drawable.angola, R.drawable.argentina,
R.drawable.armenia, R.drawable.aruba, R.drawable.australia,
R.drawable.bielorussia, R.drawable.bolivia, R.drawable.brasil,
R.drawable.cambodia, R.drawable.canada, R.drawable.china,
R.drawable.eur, R.drawable.india, R.drawable.japan,
R.drawable.libya, R.drawable.swiss, R.drawable.thai, R.drawable.uk,
R.drawable.us, R.drawable.vietnam };
TextView main_text = (TextView) mySpinner3 .findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
ImageView left_icon = (ImageView) mySpinner3 .findViewById(R.id.left_pic);
left_icon.setImageResource(arr_images[position]);
return mySpinner3;}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
void getRate(int from, int to) {
String currency;
String currency2;
currency2 = simbol[from];
int i;
for (i=0;i<=3;i=i+1){
to=money[i];
currency=simbol[to];
GetExchangeRate httpRate;
httpRate = new GetExchangeRate(currency, currency2);
Thread thr = new Thread(httpRate);
thr.start();
}}}
private void writeRate(final String text) {
runOnUiThread(new Runnable() {
@Override
public void run() {
int i;
for(i=0;i<=3;i=i+1){
values[i].setText(text);
}
}
});
}
private String readStream(InputStream in) {
BufferedReader reader = null;
StringBuffer response = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
response.append(line);
}
}
catch (IOException e) {
return e.getMessage();
}
finally {
if (reader != null) {
try {
reader.close();
}
catch (IOException e) {
return e.getMessage();
}
}
}
return response.toString();
}
private String transform(String rate) {
String readableRate = null;
JSONObject json;
try {
json = new JSONObject(rate);
readableRate = " " + json.getDouble("rate");
}
catch (JSONException e) {
readableRate = e.toString();
}
return readableRate;
}
//**************************************************************************
//Internal class to call HTTP operation in a separate thread
private class GetExchangeRate implements Runnable {
String currency;
String currency2;
GetExchangeRate(String toCurrency, String fromCurrency) {
currency = toCurrency;
currency2= fromCurrency;
}
@Override
public void run() {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL("http://rate-exchange.appspot.com/currency?from=" + currency2 + "&to=" + currency);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setUseCaches(false);
int responseCode = urlConnection.getResponseCode();
if(responseCode == HttpStatus.SC_OK) {
String response = readStream(urlConnection.getInputStream());
response = transform(response);
writeRate(response);
}
else
writeRate("Code: " + responseCode);
}
catch (Exception e) {
writeRate(e.toString());
}
finally {
if(urlConnection != null)
urlConnection.disconnect();
}
}
}
@Override
public void onClick(View v) {
money1 = Integer.parseInt(String.valueOf(spinner1.getSelectedItemPosition()));
money2 = Integer.parseInt(String.valueOf(spinner2.getSelectedItemPosition()));
money3 = Integer.parseInt(String.valueOf(spinner3.getSelectedItemPosition()));
money4 = Integer.parseInt(String.valueOf(spinner4.getSelectedItemPosition()));
int[] money = {money1,money2,money3,money4};
int i;
for(i=0; i<=3;i=i+1)
if(values[i].getText().toString().trim().length() != 0){
from=money[i];
return;}
}}
答案 0 :(得分:0)
我不完全确定,因为现在阅读你的代码并不容易,但我认为你应该在clickl的click1上调用getRate
,因为目前它是空的。
button1.setOnClickListener(this);
// here call getRate with the appropriate arguments
}
答案 1 :(得分:0)
首先是 - 我不知道为什么它不会射击你的空指针。我被绑定到手动填充文本编辑数组:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
ed3 = (EditText) findViewById(R.id.editText3);
ed4 = (EditText) findViewById(R.id.editText4);
values[0]=ed1;
values[1]=ed2;
values[2]=ed3;
values[3]=ed4;
第二件事你把getRate放在了适配器的类中。它更容易将其移动到活动类。将它移动一个支架更远 - 所以改变这个:
void getRate(int from, int to) {
String currency;
String currency2;
currency2 = simbol[from];
int i;
for (i=0;i<=3;i=i+1){
to=money[i];
currency=simbol[to];
GetExchangeRate httpRate;
httpRate = new GetExchangeRate(currency, currency2);
Thread thr = new Thread(httpRate);
thr.start();
}}}//behind last of this brackets
到此:
}
void getRate(int from, int to) {
String currency;
String currency2;
currency2 = simbol[from];
int i;
for (i=0;i<=3;i=i+1){
to=money[i];
currency=simbol[to];
GetExchangeRate httpRate;
httpRate = new GetExchangeRate(currency, currency2);
Thread thr = new Thread(httpRate);
thr.start();
}}
然后您可以在onClick侦听器中调用getRate,例如:
@Override
public void onClick(View v) {
money1 = Integer.parseInt(String.valueOf(spinner1.getSelectedItemPosition()));
money2 = Integer.parseInt(String.valueOf(spinner2.getSelectedItemPosition()));
money3 = Integer.parseInt(String.valueOf(spinner3.getSelectedItemPosition()));
money4 = Integer.parseInt(String.valueOf(spinner4.getSelectedItemPosition()));
int[] money = {money1,money2,money3,money4};
int i;
for(i=0; i<=3;i=i+1)
if(values[i].getText().toString().trim().length() != 0){
from=money[i];
getRate( from,0) ;
return;
}
}
getRate的第二个参数为零,因为您稍后将其设置为除“from”参数中的一个以外的微调器的值。所以第二个参数未使用。 我测试了它并且“工作”,但服务返回503.我真的很抱歉,我现在无法调查。 但我希望它有助于“按钮问题”。 马尔钦