필드에 금액을 입력하고 회 전자에서 from currency
을 선택하고 다른 회 전자에서 to currency
을 선택하여 USD, GBP 및 유로화에서 간단한 통화 변환기를 만들려고합니다. 그런 다음 변환 된 금액을 텍스트 필드에 출력합니다.Java Null 포인터 예외 (클릭 할 때)
하지만 어떤 이유로 나는 onClick
메서드를 통과 할 수 없으며 그 이유를 알지 못합니다. 그것은 단순한 무언가일지도 모르지만 나는 그것을 볼 수 없다. 여기
public class Calculate extends Activity implements OnItemSelectedListener,
OnClickListener {
Button Calculate;
String stringAmount;
double exchangeRate, E, D, Answer;
EditText Amount;
Spinner spinner, spinner1;
int ConvertFrom, ConvertTo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate);
Initilise();
// this is calling the method initilise.
// listens for the button to be clicked
Calculate.setOnClickListener(this);
}
private void Initilise() {
// Method initilise.
/* Button */Calculate = (Button) findViewById(R.id.cal);
/* EditText */Amount = (EditText) findViewById(R.id.amount);
spinner = (Spinner) findViewById(R.id.Currency);
spinner1 = (Spinner) findViewById(R.id.Currency1);
}
public void format() {
ConvertFrom = 0;
ConvertTo = 0;
stringAmount = Amount.getText().toString();
E = 0; // set it to 0 as the default
try {
E = Integer.parseInt(stringAmount);
} catch (NumberFormatException e) {
}
}
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
spinner();
switch (v.getId()) {
case R.id.cal:
if (ConvertFrom == 0) {
FromEuro();
} else if (ConvertFrom == 1) {
FromUsd();
} else if (ConvertFrom == 2) {
FromGbp();
} else {
// Amount.setText("Invalid Input");
AlertDialog alertDialog = new AlertDialog.Builder(this)
.create();
alertDialog.setTitle("Input");
alertDialog.setMessage("Please Input a amount");
alertDialog.setButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// here you can add functions
}
});
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.show();
}
}
}
public void spinner() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, R.array.Currencies);
spinner = (Spinner) findViewById(R.id.Currency);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
spinner1 = (Spinner) findViewById(R.id.Currency1);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int posistion = spinner.getSelectedItemPosition();
ConvertFrom = posistion;
int posistion1 = spinner1.getSelectedItemPosition();
ConvertTo = posistion1;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@SuppressWarnings("deprecation")
private void FromEuro() {
// TODO Auto-generated method stub
if (ConvertTo == 1) {
exchangeRate = 1.336891;
Answer = E * exchangeRate;
String Final = Double.toString(Answer);
Amount.setText(Final);
} else if (ConvertTo == 2) {
exchangeRate = 1.336891;
Answer = E * exchangeRate;
String Final = Double.toString(Answer);
Amount.setText(Final);
} else if (ConvertTo == 0) {
AlertDialog alertDialog = new AlertDialog.Builder(null).create();
alertDialog.setTitle("Input");
alertDialog.setMessage("Please Select a gender");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.show();
}
}
@SuppressWarnings("deprecation")
private void FromUsd() {
// TODO Auto-generated method stub
if (ConvertTo == 0) {
exchangeRate = 1.336891;
Answer = E * exchangeRate;
String Final = Double.toString(Answer);
Amount.setText(Final);
} else if (ConvertTo == 1) {
exchangeRate = 1.336891;
Answer = E * exchangeRate;
String Final = Double.toString(Answer);
Amount.setText(Final);
} else {
if (ConvertTo == 2) {
AlertDialog alertDialog = new AlertDialog.Builder(null)
.create();
alertDialog.setTitle("Input");
alertDialog.setMessage("Please Select a gender");
alertDialog.setButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// here you can add functions
}
});
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.show();
}
}
}
private void FromGbp() {
// TODO Auto-generated method stub
if (ConvertTo == 2) {
exchangeRate = 1.336891;
Answer = E * exchangeRate;
String Final = Double.toString(Answer);
Amount.setText(Final);
} else if (ConvertTo == 0) {
exchangeRate = 1.336891;
Answer = E * exchangeRate;
String Final = Double.toString(Answer);
Amount.setText(Final);
} else {
if (ConvertTo == 1) {
Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Input");
alertDialog.setMessage("Please Select a gender");
alertDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// here you can add functions
}
});
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.show();
}
}
}`
}
하지만 난 지금
02-19 16 수신하고 있습니다 : E/AndroidRuntime (28638) : android.app 55 : 01.514를 .AlertDialog.resolveDialogTheme (AlertDialog.java:142)
위의 오류. 사람이 도울 수 있다면 궁금
:
는 이전에이 질문에 다음과 같은 오류가 있었다. 당신이 코드를 게시 할 때 모든 들여 쓰기,
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.travelcurrencyconverter.Calculate.onClick(Calculate.java:64)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17273)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
질문에 com.travelcurrencyconverter.Calculate.onClick (Calculate.java:64) 행을 표시 할 수 있습니까? – Raman
코드에서 ConvertTo 또는 ConvertFrom의 값을 설정하는 것으로 보이지 않습니다. 그리고 이것이 사실이라면 onClick 메서드에서 ConvertTo 값을 사용하려고 할 때 NPE를 얻게됩니다. – Perception
나는 내 경보 대화 상자를 보여줌으로써 지금 나를 앞으로 움직이는 무언가를 잊어 버렸다는 것을 알았다. 이제 나는 회 전자를 통해 배열로부터 데이터를 얻지 못한다고 추측하지만, 거기서 나를 움직였다. D – JoeLee