我正在为android构建货币转换器并在此代码中获取空指针异常。我正在转换所有值,因为在编辑文本中输入了数字,文本视图中的所有值都被更改。请帮助!! !
public class MainActivity extends Activity {
EditText currency;
String value;
float us,pk,om,ks,e;
TextView usa,pak,uk,oman,ksa,eu;
float val;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value=currency.getText().toString();
usa=(TextView)findViewById(R.id.textView1);
pak=(TextView)findViewById(R.id.textView2);
uk=(TextView)findViewById(R.id.textView3);
oman=(TextView)findViewById(R.id.textView4);
ksa=(TextView)findViewById(R.id.textView5);
eu=(TextView)findViewById(R.id.textView6);
currency=(EditText)findViewById(R.id.editText1);
currency.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
value=currency.getText().toString();
// Toast.makeText(getApplicationContext(),value, Toast.LENGTH_SHORT).show();
try {
val = Float.parseFloat(value);
us=val*3.67f;
usa.setText(Float.toString(us));
pk=val=26.95f*val;
pak.setText(Float.toString(pk));
om=val*0.10f;
oman.setText(Float.toString(om));
ks=val*1.02f;
ksa.setText(Float.toString(ks));
e=val*0.20f;
eu.setText(Float.toString(e));
}
catch(NumberFormatException ex) {
val = 0.0f;
usa.setText("0");
pak.setText("0");
uk.setText("0");
oman.setText("0");
ksa.setText("0");
eu.setText("0");
}
Toast.makeText(getApplicationContext(),Float.toString(val), Toast.LENGTH_SHORT).show();
//us=val*3.67;
}
public void beforeTextChanged(CharSequence s, int start, int count,
int aft
er) {
}
public void afterTextChanged(Editable s) {
}
});
//val=Double.parseDouble(value);
// us=val*3.67;
// usa.setText(String.valueOf(us));
}
跟踪:
08-12 17:35:03.846: E/AndroidRuntime(2944): FATAL EXCEPTION: main
08-12 17:35:03.846: E/AndroidRuntime(2944): java.lang.RuntimeException: Unable to start activity ComponentInfo{omnia.android.nfc/omnia.android.nfc.MainActivity}: java.lang.NullPointerException
08-12 17:35:03.846: E/AndroidRuntime(2944): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
08-12 17:35:03.846: E/AndroidRuntime(2944): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-12 17:35:03.846: E/AndroidRuntime(2944): at android.app.ActivityThread.access$700(ActivityThread.java:140)
08-12 17:35:03.846: E/AndroidRuntime(2944): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
08-12 17:35:03.846: E/AndroidRuntime(2944): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 17:35:03.846: E/AndroidRuntime(2944): at android.os.Looper.loop(Looper.java:137)
08-12 17:35:03.846: E/AndroidRuntime(2944): at android.app.ActivityThread.main(ActivityThread.java:4921)
08-12 17:35:03.846: E/AndroidRuntime(2944): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 17:35:03.846: E/AndroidRuntime(2944): at java.lang.reflect.Method.invoke(Method.java:511)
08-12 17:35:03.846: E/AndroidRuntime(2944): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
答案 0 :(得分:3)
...
currency=(EditText)findViewById(R.id.editText1);
value=currency.getText().toString();
usa=(TextView)findViewById(R.id.textView1);
pak=(TextView)findViewById(R.id.textView2);
uk=(TextView)findViewById(R.id.textView3);
oman=(TextView)findViewById(R.id.textView4);
ksa=(TextView)findViewById(R.id.textView5);
eu=(TextView)findViewById(R.id.textView6);
...
在此处移动货币。
您正试图在不启动货币数据的情况下获取货币数据。