从edittext-Android 2获取文本时出错

时间:2014-11-26 18:19:59

标签: android textview

我正在做一个货币转换器,我想只引入一个值,该值是用户想要的硬币转换。当我运行程序并单击底部转换它关闭应用程序并在logcat中出现error- java.lang.NullPointerException:尝试调用虚方法' android.text.Editable android.widget.EditText。的getText()'在null对象引用上。

我不知道错误在哪里,而且我是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;}

 }}

2 个答案:

答案 0 :(得分:1)

在这一行

if(values[i].getText().toString().trim().length() != 0){

values[i]至少null值为i,因此在getText()引用上调用null。首先尝试初始化values中的元素。检查ed1ed2ed3ed4的初始化。

答案 1 :(得分:1)

使用行

处的空值初始化值数组
EditText[] values = {ed1,ed2,ed3,ed4};

此时ed1,ed2 ...未初始化。然后用空值填充数组。实际上,你不需要ed变量。只需替换行

ed1 = (EditText) findViewById(R.id.editText1);
...

使用

values[0] = (EditText) findViewById(R.id.editText1);