0

我正在制作一个类似于测验的应用程序。

当您选择答案并在后台将其提供给下一个时,应发送响应 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 是强制性的吗?

非常感谢

4

2 回答 2

1

问题是您试图doInBackground()在此方法中禁止 UI 操作时显示 Toast。

您应该做的是在此方法中进行所有处理,返回处理结果,然后在onPostExecute()必要时将其处理并显示 Toast。

编辑

类似的东西:改变返回值writeXML

public String writeXML(Context ctx) throws IOException{   
  FileOutputStream fout = null;

  try {
     fout = ctx.openFileOutput("answers.xml", Context.MODE_PRIVATE);
  } catch (FileNotFoundException e) {
     return e.getMessage();
  }

  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();

     return "write done";
  } catch (Exception e) {
     return e.getMessage();
  }
  }

还要更改 : 的返回值doInBackground(您还必须更改您的AsyncTask声明class AsyncLoadXML extends AsyncTask<Void, Integer, String>:)

   @Override

   protected String doInBackground(Void... params) {
       String result = "";

       try {
           result = writeXML(ctx);
       } catch (IOException e) {

          e.printStackTrace();
       }

       return result;
   }

并添加onPostExecute方法:

protected void onPostExecute(String result) {
    if(!result.equals("")) 
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
于 2013-09-15T19:05:32.247 回答
0

查看您的日志:

Can't create handler inside thread that has not called Looper.prepare()

添加Looper.prepare();到您运行 Handler 的线程

或者

通过处理程序对 Thread UI 使用迭代,例如:

private Handler mHandler = null;
protected static final int TOAST = 1;

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        String txt;
        switch (msg.what) {
        case TOAST:
            txt = msg.obj.toString();
            Toast.makeText(ctx, txt, Toast.LENGTH_LONG).show();
            break;          
        default:
            super.handleMessage(msg);
        }           



    }
};

现在您可以使用处理程序显示您的祝酒词:

mHandler.sendMessage(mHandler.obtainMessage(TOAST, "write done"));

或者

就像 jbihan 说的,把你的逻辑放在你展示 Toast 的地方onPostExecute()

因为在 Toast 下doInBackgrounddrop 你的错误

于 2013-09-15T19:04:55.887 回答