我认为无法确保提供所有信息然后列出我的所有三个课程。但是,在所有这些之前,我将包括我的logcat错误以及我的应用程序应该做的事情:
03-27 17:10:45.368 2373-2665/edu.ggc.amauldin.currencyappaustin E/AndroidRuntime:
FATAL EXCEPTION: AsyncTask #1
Process: DOMAIN, PID: 2373
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
'void java.util.Scanner.close()' on a null object reference
at DOMAIN.FindRate.doInBackground(FindRate.java:59)
at DOMAIN.FindRate.doInBackground(FindRate.java:25)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
用户在EditText字段中输入一个值,然后选择一个基础货币和一个转换为2个微调器的货币,然后按下转换按钮并输出新货币。我正在尝试使用异步任务和fixer.io来完成此任务,希望学习如何更好地理解和使用它们。
我将列出我的课程,看看是否能更好地阐明我的情况。如果需要,我会省略我的进口等,以减少这篇文章的一面,让我知道。
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
public static double sum;
private static String urlIO = "http://api.fixer.io/latest?base=";
Spinner spinner, spinner2;
Button convBtn, aboutBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final EditText enteredAmt = (EditText)findViewById(R.id.editText);
final TextView result = (TextView)findViewById(R.id.resultTv);
spinner = (Spinner) findViewById(R.id.spinner);
spinner2 = (Spinner) findViewById(R.id.spinner2);
convBtn = (Button) findViewById(R.id.button);
aboutBtn = (Button) findViewById(R.id.button2);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.currencyTypes, android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner2.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
convBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FindRate covertMe = new FindRate(result);
sum = Double.parseDouble(enteredAmt.getText().toString());
try{
if(spinner.getSelectedItem().toString().equals("USD") && spinner2.getSelectedItem().toString().equals("EUR")){
covertMe.execute(new URL(urlIO + "USD&symbols=GBP"));
}
} catch (MalformedURLException e){
e.printStackTrace();
}
}
});
aboutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "By: Austin Mauldin \n ITEC 4550", Toast.LENGTH_SHORT).show();
}
});
}
public class FindRate extends AsyncTask<URL, Void, Double> {
private static final String rightForm = "###.00";
private static final String errorCode = "error code: it messed up";
private static final String checkRSP = "Checking";
private TextView tV;
private Gson gSON;
FindRate(TextView inp){
tV = inp;
gSON = new GsonBuilder().create();
}
@Override
protected Double doInBackground(URL... params) {
HttpURLConnection connectStat = null;
Scanner scanner = null;
StringBuilder myJsonString = new StringBuilder();
try {
if (params.length != 1)
throw new IllegalArgumentException(this.getClass().getName() + errorCode);
connectStat = (HttpURLConnection) params[0].openConnection();
InputStream inputer = new BufferedInputStream(connectStat.getInputStream());
scanner = new Scanner(inputer);
while (scanner.hasNext()) myJsonString.append(scanner.nextLine());
Log.v(checkRSP, "Response(" + connectStat.getResponseCode() + "):" +
connectStat.getResponseMessage());
} catch (IOException e){
Log.e(checkRSP, e.getStackTrace().toString());
return Double.valueOf(-1D);
} finally {
scanner.close();
connectStat.disconnect();
}
String jSon = myJsonString.toString();
LookupRate cuRate = gSON.fromJson(jSon, LookupRate.class);
Log.v(checkRSP, jSon);
Log.v(checkRSP, cuRate.toString());
return Double.valueOf(cuRate.findRate());
}
@Override
protected void onPostExecute(Double result){
DecimalFormat format = new DecimalFormat(rightForm);
String str = format.format(result);
tV.setText(format.format(MainActivity.sum * Double.parseDouble(str))+ "");
}
}
这是另一个我不喜欢的领域我想从fixer.io输出中获取rate(s)值,所以如果它有帮助,那就是fixer.io输出看起来像{{3} }
public class LookupRate {
private String to;
private Double rate;
private String from;
public Double findRate() {
return rate;
}
@Override
public String toString(){
String str1 = "Checking" +
" [rates=" + rate + "]";
return str1;
}
}
应用程序因点击转换按钮而崩溃只是为了确保完全清楚。我在这做错了什么?
答案 0 :(得分:1)
更改
finally {
scanner.close();
connectStat.disconnect();
}
到
finally {
if(null != scanner){ scanner.close();}
if(null != connectStat){ connectStat.disconnect();}
}
我猜测在初始化扫描程序之前会抛出异常,因此会出现空指针异常。黄金法则,总是检查空