我正在制作一个类似于测验的应用程序。
当您选择答案并在后台将其提供给下一个时,应发送响应 xml 文件并移至下一个问题(活动)
这是调用后台任务并传递第二个活动的活动的代码
if(options == "0") ans.setText("Must select an answer");
else {
Intent intent =
new Intent(PM1_1Activity.this, PM1_2Activity.class);
AsyncLoadXML carga = new AsyncLoadXML(PM1_1Activity.this);
carga.execute();
startActivity(intent);
finish();
}
这是创建后台任务的类的代码
package com.asde.ipac;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Xml;
import android.widget.Toast;
class AsyncLoadXML extends AsyncTask<Void, Integer, Boolean> {
Context ctx;
public AsyncLoadXML(Context c) {
this.ctx = c;
}
@Override
protected Boolean doInBackground(Void... params) {
try {
writeXML(ctx);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void writeXML(Context ctx) throws IOException{
FileOutputStream fout = null;
try {
fout = ctx.openFileOutput("answers.xml", Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show();
}
XmlSerializer serializer = Xml.newSerializer();
try {
serializer.setOutput(fout, "UTF-8");
serializer.startDocument(null, true);
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, "questions");
serializer.startTag(null, "question");
serializer.attribute(null,"grupo" , "test");
serializer.attribute(null,"grupo" , "test");
serializer.text("A");
serializer.endTag(null, "question");
serializer.endTag(null, "questions");
serializer.endDocument();
serializer.flush();
fout.close();
Toast.makeText(ctx, "write done", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
这是 LogCat 的输出
09-15 14:34:04.151: E/AndroidRuntime(1225): FATAL EXCEPTION: AsyncTask #5
09-15 14:34:04.151: E/AndroidRuntime(1225): java.lang.RuntimeException: An error occured while executing doInBackground()
09-15 14:34:04.151: E/AndroidRuntime(1225): at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-15 14:34:04.151: E/AndroidRuntime(1225): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
09-15 14:34:04.151: E/AndroidRuntime(1225): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
09-15 14:34:04.151: E/AndroidRuntime(1225): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
09-15 14:34:04.151: E/AndroidRuntime(1225): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-15 14:34:04.151: E/AndroidRuntime(1225): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-15 14:34:04.151: E/AndroidRuntime(1225): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-15 14:34:04.151: E/AndroidRuntime(1225): at java.lang.Thread.run(Thread.java:841)
09-15 14:34:04.151: E/AndroidRuntime(1225): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-15 14:34:04.151: E/AndroidRuntime(1225): at android.os.Handler.<init>(Handler.java:197)
09-15 14:34:04.151: E/AndroidRuntime(1225): at android.os.Handler.<init>(Handler.java:111)
09-15 14:34:04.151: E/AndroidRuntime(1225): at android.widget.Toast$TN.<init>(Toast.java:324)
09-15 14:34:04.151: E/AndroidRuntime(1225): at android.widget.Toast.<init>(Toast.java:91)
09-15 14:34:04.151: E/AndroidRuntime(1225): at android.widget.Toast.makeText(Toast.java:238)
09-15 14:34:04.151: E/AndroidRuntime(1225): at com.asde.ipac.AsyncLoadXML.escribirXML(AsyncLoadXML.java:96)
09-15 14:34:04.151: E/AndroidRuntime(1225): at com.asde.ipac.AsyncLoadXML.doInBackground(AsyncLoadXML.java:43)
09-15 14:34:04.151: E/AndroidRuntime(1225): at com.asde.ipac.AsyncLoadXML.doInBackground(AsyncLoadXML.java:1)
09-15 14:34:04.151: E/AndroidRuntime(1225): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-15 14:34:04.151: E/AndroidRuntime(1225): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-15 14:34:04.151: E/AndroidRuntime(1225): ... 4 more
不,我真的做错了。方法 OnPostExecute 是强制性的吗?
非常感谢