我在AsyncTask
写下了以下代码,但是错误并说:
new AsyncTask<Void,Void,String>(){
@Override
protected String doInBackground(Void... params) {
long result = 0;
long t = System.currentTimeMillis();
HERE---->switch (StartActivity.this.RadioType.getCheckedRadioButtonId()){
case R.id.radioFibJR:
result = FibLib.fibJR(n);
break;
case R.id.radioFibJI:
result = FibLib.fibJI(n);
break;
case R.id.radioFibNR:
result = FibLib.fibNR(n);
break;
case R.id.radioFibNI:
result = FibLib.fibNI(n);
break;
}
t = System.currentTimeMillis() - t;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}.execute();
答案 0 :(得分:1)
将该代码放在AsyncTask之外。这就是诀窍。干杯:)
int id=StartActivity.this.RadioType.getCheckedRadioButtonId();
new AsyncTask<Void,Void,String>(){
@Override
protected String doInBackground(Void... params) {
long result = 0;
long t = System.currentTimeMillis();
switch (id){
case R.id.radioFibJR:
result = FibLib.fibJR(n);
break;
case R.id.radioFibJI:
result = FibLib.fibJI(n);
break;
case R.id.radioFibNR:
result = FibLib.fibNR(n);
break;
case R.id.radioFibNI:
result = FibLib.fibNI(n);
break;
}
t = System.currentTimeMillis() - t;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}.execute();