关于Android的一个程序问题,货币转换
============问题描述============
这是界面<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/text2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Convert this amount:"> </TextView> <EditText android:id="@+id/edit1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number"> </EditText> <TextView android:id="@+id/text3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="From:"> </TextView> <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content">" </Spinner> <TextView android:id="@+id/text4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="To:"> </TextView> <Spinner android:id="@+id/spinner2" android:layout_width="match_parent" android:layout_height="wrap_content"> </Spinner> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Convert">" </Button> <TextView android:id="@+id/text5" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25px" android:gravity="center"> </TextView> </LinearLayout>这是界面的XML
package com.exercise2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.*;
public class Currency extends Activity
{
private EditText cedit;
private Spinner cfrom;
private Spinner cto;
private Button cconvert;
private TextView ctext;
private Toast toast;
private ArrayAdapter<String> madapter1;
private ArrayAdapter<String> madapter2;
private String[] cdatafrom=
{
"-Please Select-",
"CNY","USD","GBP","EUR","JPY","HKD"
};
private String[] cdatato=
{
"-Please Select-",
"CNY","USD","GBP","EUR","JPY","HKD"
};
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.currencyconverter);
cedit=(EditText)findViewById(R.id.edit2);
cconvert=(Button)findViewById(R.id.button3);
ctext=(TextView)findViewById(R.id.text5);
cfrom=(Spinner)findViewById(R.id.spinner1);
madapter1=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,cdatafrom);
madapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cfrom.setAdapter(madapter1);
cto=(Spinner)findViewById(R.id.spinner2);
madapter2=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,cdatato);
madapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cto.setAdapter(madapter2);
//cedit.setInputType(EditorInfo.TYPE_CLASS_PHONE);
cconvert.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
String str=cedit.getText().toString();
double curr=Double.parseDouble(str);
while(!str.equalsIgnoreCase(""))
{
String from=cfrom.getSelectedItem().toString();
String to=cto.getSelectedItem().toString();
if(cfrom.getSelectedItem().toString().equalsIgnoreCase(cto.getSelectedItem().toString()))
{
ctext.setText("ssss");
//ctext.setText(str+" "+cfrom.getSelectedItem().toString());
}
else
{
if(cfrom.getSelectedItem().toString().equalsIgnoreCase("CNY"))
{
if(cto.getSelectedItem().toString().equalsIgnoreCase("USD"))
{
double curr1=curr*0.1631;
ctext.setText(curr1+" USD");
}
if(cto.getSelectedItem().toString().equalsIgnoreCase("GBP"))
{
double curr1=curr*0.1008;
ctext.setText(curr1+" GBP");
}
if(cto.getSelectedItem().toString().equalsIgnoreCase("EUR"))
{
double curr1=curr*0.1283;
ctext.setText(curr1+" EUR");
}
if(cto.getSelectedItem().toString().equalsIgnoreCase("JPY"))
{
double curr1=curr*17.5914;
ctext.setText(curr1+" JPY");
}
if(cto.getSelectedItem().toString().equalsIgnoreCase("HKD"))
{
double curr1=curr*1.2653;
ctext.setText(curr1+" HKD");
}
}
while(from=="USD")
{
if(to=="CNY")
{
double curr1=curr*6.1298;
ctext.setText(curr1+" "+to);
}
if(to=="GBP")
{
double curr1=curr*0.6179;
ctext.setText(curr1+" "+to);
}
if(to=="EUR")
{
double curr1=curr*0.7863;
ctext.setText(curr1+" "+to);
}
if(to=="JPY")
{
double curr1=curr*107.8320;
ctext.setText(curr1+" "+to);
}
if(to=="HKD")
{
double curr1=curr*7.7561;
ctext.setText(curr1+" "+to);
}
}
while(from=="GBP")
{
if(to=="CNY")
{
double curr1=curr*9.9204;
ctext.setText(curr1+" "+to);
}
if(to=="USD")
{
double curr1=curr*1.6184;
ctext.setText(curr1+" "+to);
}
if(to=="EUR")
{
double curr1=curr*1.2725;
ctext.setText(curr1+" "+to);
}
if(to=="JPY")
{
double curr1=curr*174.5137;
ctext.setText(curr1+" "+to);
}
if(to=="HKD")
{
double curr1=curr*12.5524;
ctext.setText(curr1+" "+to);
}
}
}
}
while(str.equalsIgnoreCase(""))
{
toast=Toast.makeText(Currency.this, "Enter The Value", Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
}这是部分代码但是会有错,停止运行。。。
这是Logcat红色部分截图。我自己想了很久,刚学水平有限啊。。
求大大帮忙看看
============解决方案1============
edit2在xml布局文件中没有定义,导致cedit对象为null
============解决方案2============
cedit=(EditText)findViewById(R.id.edit2);中R.id.edit2换成R.id.edit1就行了
浙公网安备 33010602011771号