Android アプリケーションを作成していますが、onTextChanged エラーが発生しています。アプリケーションは通貨コンバーターです。テキストに金額を入力しようとすると、アプリケーションがクラッシュします。
ここに私のコードがあります:
public class Currency_convert extends Activity
{
/** Called when the activity is first created. */
float inputValue;
Handler h;
static int decimalsize = 2;
public DecimalFormat df = new DecimalFormat();
String US = "USD";//us dollar
String EURO = "EUR";
String INR = "INR";//Indian rupee
String AUD = "AUD";//australian dollar
String GBP = "GBP";//Pound sterling
String ILS = "ILS";
String TRY = "TRY";
String RUB = "RUB";
String JPY = "JPY";
String MYR = "MYR";
public Double dUS;// = 0.0;
public Double dEURO;// = 1.0;
public Double dINR;// = 0.0;
public Double dAUD;// = 0.0;
public Double dGBP;// = 0.0;
public Double dILS;// = 0.0;
public Double dTRY;
public Double dRUB;
public Double dJPY;
public Double dMYR;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.currency);
// setHeader(getString(R.string.currency), true);
boolean rateupdate = false;
AbsoluteLayout al = (AbsoluteLayout) findViewById(R.id.AbsoluteLayout01);
al.setBackgroundColor(Color.LTGRAY);//
final EditText amount = (EditText) findViewById(R.id.txt1);
final EditText etResult = (EditText) findViewById(R.id.txt2);
final Spinner sp1=(Spinner) findViewById(R.id.spinerr1);
final Spinner sp2=(Spinner) findViewById(R.id.spinerr2);
final InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.currency, android.R.layout.simple_spinner_item);
final Button btn = (Button) findViewById(R.id.bb1);
amount.setText("0.0");
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter);
sp2.setAdapter(adapter);
rateupdate = getRate();
if(rateupdate)
btn.setText("Currency Rate updated");
else
btn.setText("Updated Currency Rate ");
//btn.setText(Boolean.toString(rateupdate));
OnClickListener ltouch = new OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
//getRate();
}
};;;
btn.setOnClickListener(ltouch );
OnItemClickListener itemClick = new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
int curr1 = (int) sp1.getSelectedItemId();
int curr2 = (int) sp2.getSelectedItemId();
double result = Convert(curr1, curr2, amount.getText().toString());
etResult.setText(Double.toString(result));
}
};
OnItemSelectedListener ItemSelectListener = new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id)
{
try
{
int curr1 = (int) sp1.getSelectedItemId();
int curr2 = (int) sp2.getSelectedItemId();
double result = Convert(curr1, curr2, amount.getText().toString());
etResult.setText(Double.toString(result));
}
catch(Exception e)
{
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
};
sp1.setOnItemSelectedListener(ItemSelectListener);
sp2.setOnItemSelectedListener(ItemSelectListener);
amount.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
int length = s.length();
if(length == 0)
//amount.setText("0.0");
s = "0.0";
int curr1 = (int) sp1.getSelectedItemId();
int curr2 = (int) sp2.getSelectedItemId();
//double result = Convert(curr1, curr2, amount.getText().toString());
double result = Convert(curr1, curr2, s.toString());
etResult.setText(Double.toString(result));
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after)
{
}
@Override
public void afterTextChanged(Editable s)
{
// TODO Auto-generated method stub
}
});
OnTouchListener l = new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
imm.hideSoftInputFromWindow(amount.getWindowToken(),0);//
return true;
}
};
al.setOnTouchListener(l);
sp1.setSelection(4);
sp2.setSelection(0);
}
protected Double Convert(int currency_from_index, int currency_to_index, String s_amount)
{
// TODO Auto-generated method stub
Double result = 0.0;
Double amount = Double.parseDouble(s_amount);
Double source = 0.0;
Double destination = 0.0;
if((dUS * dAUD * dINR * dGBP * dEURO * dTRY * dRUB * dJPY * dMYR) == 0)
{
result = 0.0;
return result;
}
else
{
switch (currency_from_index)
{
case 0:
source = dUS;
break;
case 1:
source = dAUD;
break;
case 2:
source = dINR;
break;
case 3:
source = dGBP;
break;
case 4:
source = dEURO;
break;
case 5:
source = dILS;
break;
case 6:
source = dTRY;
break;
case 7:
source = dRUB;
break;
case 8:
source = dJPY;
break;
case 9:
source = dMYR;
break;
default:
break;
}
switch (currency_to_index)
{
case 0:
destination = dUS;
break;
case 1:
destination = dAUD;
break;
case 2:
destination = dINR;
break;
case 3:
destination = dGBP;
break;
case 4:
destination = dEURO;
break;
case 5:
destination =dILS;
break;
case 6:
destination =dTRY;
break;
case 7:
destination =dRUB;
break;
case 8:
destination =dJPY;
break;
case 9:
destination =dMYR;
break;
default:
break;
}
result = amount*destination/source;
Math.round(result);
try
{
df.setMaximumFractionDigits(decimalsize);
df.setMinimumFractionDigits(decimalsize);
String str_result = df.format(result);
result = Double.parseDouble(str_result);
}
catch(Exception e)
{
}
return result;
}
}
public boolean getRate()
{
//ConversionRate cr = new ConversionRate();
try
{
//URL url = new URL(eText.getText().toString());
String path = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
URL url = new URL(path);
URLConnection conn = url.openConnection();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
//xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
xpp.setInput(rd);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_DOCUMENT)
{
//System.out.println("Start document");
//tView.append("Start document");
}
else if(eventType == XmlPullParser.END_DOCUMENT)
{
//System.out.println("End document");
//tView.append("End document");
}
else if(eventType == XmlPullParser.START_TAG)
{
//System.out.println("Start tag "+xpp.getName());
//tView.append("Start tag "+xpp.getName());
//tView.append("Attribute Count = " + xpp.getAttributeCount());
if(xpp.getAttributeCount() > 1)
{
/*tView.append("Currency = " + xpp.getAttributeValue(0) + "\t");
tView.append("Rate = " + xpp.getAttributeValue(1));
tView.append("\n");*/
//cr.setName(xpp.getAttributeValue(0));
//cr.setRate(Double.parseDouble(xpp.getAttributeValue(1)));
if(xpp.getAttributeValue(0).equals(US))// toString()== US)
Currency_convert.this.dUS = Double.parseDouble(xpp.getAttributeValue(1));
else if(xpp.getAttributeValue(0).equals(INR))// .toString() == "INR")
dINR = Double.parseDouble(xpp.getAttributeValue(1));
else if(xpp.getAttributeValue(0).equals(GBP))// .toString() == "GBP")
dGBP = Double.parseDouble(xpp.getAttributeValue(1));
else if(xpp.getAttributeValue(0).equals(AUD))// .toString() == "AUD")
dAUD = Double.parseDouble(xpp.getAttributeValue(1));
else if(xpp.getAttributeValue(0).equals(ILS))
dILS = Double.parseDouble(xpp.getAttributeValue(1));
else if(xpp.getAttributeValue(0).equals(TRY))
dTRY = Double.parseDouble(xpp.getAttributeValue(1));
else if(xpp.getAttributeValue(0).equals(RUB))
dRUB = Double.parseDouble(xpp.getAttributeValue(1));
else if(xpp.getAttributeValue(0).equals(JPY))
dJPY = Double.parseDouble(xpp.getAttributeValue(1));
else if(xpp.getAttributeValue(0).equals(MYR))
dMYR = Double.parseDouble(xpp.getAttributeValue(1));
}
}
else if(eventType == XmlPullParser.END_TAG)
{
//System.out.println("End tag "+xpp.getName());
//tView.append("End tag "+xpp.getName());
}
else if(eventType == XmlPullParser.TEXT)
{
//System.out.println("Text "+xpp.getText());
//tView.append("Text "+xpp.getText());
}
eventType = xpp.next();
}
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
}
そして、ここに私が得ているlogcatがあります:
11-06 05:59:49.208: E/AndroidRuntime(5314): FATAL EXCEPTION: main
11-06 05:59:49.208: E/AndroidRuntime(5314): java.lang.NullPointerException
11-06 05:59:49.208: E/AndroidRuntime(5314): at com.example.dashboard_our.Currency_convert.Convert(Currency_convert.java:209)
11-06 05:59:49.208: E/AndroidRuntime(5314): at com.example.dashboard_our.Currency_convert$4.onTextChanged(Currency_convert.java:171)
11-06 05:59:49.208: E/AndroidRuntime(5314): at android.widget.TextView.sendOnTextChanged(TextView.java:7318)
11-06 05:59:49.208: E/AndroidRuntime(5314): at android.widget.TextView.handleTextChanged(TextView.java:7377)
11-06 05:59:49.208: E/AndroidRuntime(5314): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9076)
11-06 05:59:49.208: E/AndroidRuntime(5314): at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
11-06 05:59:49.208: E/AndroidRuntime(5314): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
11-06 05:59:49.208: E/AndroidRuntime(5314): at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:212)
11-06 05:59:49.208: E/AndroidRuntime(5314): at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:30)
11-06 05:59:49.208: E/AndroidRuntime(5314): at android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:94)
11-06 05:59:49.208: E/AndroidRuntime(5314): at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:49)
11-06 05:59:49.208: E/AndroidRuntime(5314): at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:155)
そしてこれはxmlファイルです:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="@+id/AbsoluteLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText android:id="@+id/txt1"
android:text="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_y="10dip"
android:numeric="decimal"
android:singleLine="true"
android:ems="8"
android:hint="Enter amount"
android:layout_x="5dip">
</EditText>
<EditText android:id="@+id/txt2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@+id/EditText02"
android:editable="false"
android:enabled="true"
android:singleLine="true"
android:layout_x="5dip"
android:ems="8"
android:layout_y="69dip">
</EditText>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/bb1"
android:layout_x="50dip"
android:text="Update Currency Rates"
android:layout_y="120dip"></Button>
<Spinner
android:id="@+id/spinerr1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="182dp"
android:layout_y="-14dp" />
<Spinner
android:id="@+id/spinerr2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="176dp"
android:layout_y="47dp" />
</AbsoluteLayout>