Java 创建异步任务时出现问题。arraylist中的文本文件

Java 创建异步任务时出现问题。arraylist中的文本文件,java,android,Java,Android,我在创建在后台运行的方法时遇到一些问题。我正在使用Androidstudiojava,希望创建一个方法,在数组列表中加载文本文件。我尝试在一个独立于activity类的类中的一个公共方法中实现这一点。当我运行应用程序时,我会遇到一些问题,表明主线程存在问题,因此我想创建一个asynctask。我在互联网上找遍了,但找不到任何相关的东西。既然我是初学者,请询问是否有不清楚的地方。感谢您的帮助 这来自一个类,该类将向第一个代码下面的public void CreateQuestion()发送一个变量

我在创建在后台运行的方法时遇到一些问题。我正在使用Android
studiojava
,希望创建一个方法,在
数组列表中加载文本文件。我尝试在一个独立于activity类的类中的一个公共方法中实现这一点。当我运行应用程序时,我会遇到一些问题,表明主线程存在问题,因此我想创建一个
asynctask
。我在互联网上找遍了,但找不到任何相关的东西。既然我是初学者,请询问是否有不清楚的地方。感谢您的帮助

这来自一个类,该类将向第一个代码下面的
public void CreateQuestion()
发送一个变量:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_action);

    Intent callingIntent = getIntent();
    int index = callingIntent.getIntExtra("INDEX",0);      


    if(index==0){
        mQuestionBox = new QuestionBox();
        try {
            mQuestionBox.createQuestions("hogskoleprovet.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    else if(index==1){
        mQuestionBox = new QuestionBox();
        try {
            mQuestionBox.createQuestions("hogskoleprovet.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    else if(index==2){
        mQuestionBox = new QuestionBox();
        try {
            mQuestionBox.createQuestions("hogskoleprovet.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
我想把这段代码作为一个异步任务,但我不知道怎么做

public void createQuestions(String hogskoleprovet)  throws IOException {


    InputStream iS = sContext.getAssets().open(hogskoleprovet);
    BufferedReader reader = new BufferedReader(new InputStreamReader(iS));

    mQuestions = new ArrayList<Question>();


    String question, answer, answerOne, answerTwo, answerThree, answerFour;



    while (reader.readLine() != null) {

        //reading some lines from resource file
        question = reader.readLine();
        answer = reader.readLine();
        answerOne = reader.readLine();
        answerTwo = reader.readLine();
        answerThree = reader.readLine();
        answerFour = reader.readLine();
        Question q = new Question(question, answer, answerOne, answerTwo, answerThree, answerFour);
        mQuestions.add(q);
        break;
    }

    reader.close();


}
public void createQuestions(字符串hogskoleprovet)引发IOException{
InputStream=sContext.getAssets().open(hogskoleprovet);
BufferedReader reader=新的BufferedReader(新的InputStreamReader(iS));
mQuestions=newarraylist();
串问题,回答,回答一,回答二,回答三,回答四;
while(reader.readLine()!=null){
//从资源文件中读取一些行
问题=reader.readLine();
答案=reader.readLine();
answerOne=reader.readLine();
answerTwo=reader.readLine();
answerThree=reader.readLine();
answerFour=reader.readLine();
问题q=新问题(问题、答案、答案一、答案二、答案三、答案四);
mQuestions.add(q);
打破
}
reader.close();
}

尝试修改createQuestions方法,如下所示:

public void createQuestions(final String hogskoleprovet) throws IOException {

new AsyncTask<Integer, Integer, Boolean>()
    {

        @Override
        protected void onPreExecute()

        {

        }

        @Override
        protected Boolean doInBackground(Integer… params)
        {

          InputStream iS = sContext.getAssets().open(hogskoleprovet);
          BufferedReader reader = new BufferedReader(new InputStreamReader(iS)); 

        mQuestions = new ArrayList<Question>();


        String question, answer, answerOne, answerTwo, answerThree, answerFour;



  while (reader.readLine() != null) {

       //reading some lines from resource file
       question = reader.readLine();
       answer = reader.readLine();
       answerOne = reader.readLine();
       answerTwo = reader.readLine();
       answerThree = reader.readLine();
       answerFour = reader.readLine();
       Question q = new Question(question, answer, answerOne, answerTwo, answerThree, answerFour);
      mQuestions.add(q);
      break;
  }

  reader.close();


            return true;
        }

        @Override
        protected void onPostExecute(Boolean result)
        {




        }
    }.execute();
 }
public void createQuestions(最终字符串hogskoleprovet)引发IOException{
新建异步任务()
{
@凌驾
受保护的void onPreExecute()
{
}
@凌驾
受保护的布尔doInBackground(整数…参数)
{
InputStream=sContext.getAssets().open(hogskoleprovet);
BufferedReader reader=新的BufferedReader(新的InputStreamReader(iS));
mQuestions=newarraylist();
串问题,回答,回答一,回答二,回答三,回答四;
while(reader.readLine()!=null){
//从资源文件中读取一些行
问题=reader.readLine();
答案=reader.readLine();
answerOne=reader.readLine();
answerTwo=reader.readLine();
answerThree=reader.readLine();
answerFour=reader.readLine();
问题q=新问题(问题、答案、答案一、答案二、答案三、答案四);
mQuestions.add(q);
打破
}
reader.close();
返回true;
}
@凌驾
受保护的void onPostExecute(布尔结果)
{
}
}.execute();
}

Asynctask类从文件中读取数据

public class FileReader_async extends AsyncTask{
private Context context;
private Callback callback;
private List<Question> mQuestions;
    public FileReader_async(Context context,Callback callback)
    {
        this.callback=callback;
    }
    @Override
    protected Object doInBackground(Object... params) {
        InputStream iS = null;
        try {
            iS = context.getAssets().open("filename");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(iS));

        mQuestions = new ArrayList<Question>();


        String question, answer, answerOne, answerTwo, answerThree, answerFour;



        try {
            while (reader.readLine() != null) {

                //reading some lines from resource file
                question = reader.readLine();
                answer = reader.readLine();
                answerOne = reader.readLine();
                answerTwo = reader.readLine();
                answerThree = reader.readLine();
                answerFour = reader.readLine();
                Question q = new Question(question, answer, answerOne, answerTwo, answerThree, answerFour);
                mQuestions.add(q);
                break;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            reader.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }
    @Override
    protected void onPostExecute(Object result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        callback.notify_result(mQuestions);
    }



}
公共类FileReader\u async扩展了AsyncTask{
私人语境;
私有回调;
私有列表mQuestions;
公共文件读取器异步(上下文上下文,回调)
{
this.callback=callback;
}
@凌驾
受保护对象doInBackground(对象…参数){
InputStream=null;
试一试{
iS=context.getAssets().open(“文件名”);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
BufferedReader reader=新的BufferedReader(新的InputStreamReader(iS));
mQuestions=newarraylist();
串问题,回答,回答一,回答二,回答三,回答四;
试一试{
while(reader.readLine()!=null){
//从资源文件中读取一些行
问题=reader.readLine();
答案=reader.readLine();
answerOne=reader.readLine();
answerTwo=reader.readLine();
answerThree=reader.readLine();
answerFour=reader.readLine();
问题q=新问题(问题、答案、答案一、答案二、答案三、答案四);
mQuestions.add(q);
打破
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
reader.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回0;
}
@凌驾
受保护的void onPostExecute(对象结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
回调。通知_结果(mQuestions);
}
}
用于在asnyctask完成时通知的回调接口

public interface Callback {

    public void notify_result(List<Question> question_list);


}
公共接口回调{
公共无效通知结果(列表问题列表);
}
调用asynctask从文件读取数据的活动

public class MainActivity extends ActionBarActivity implements Callback{
private  FileReader_async fileReader_async;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //invoke the asynctask when you want to read the question.. to invoke asynctask code 

        fileReader_async=new FileReader_async(getApplicationContext(), this);
        fileReader_async.execute();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void notify_result(List<Question> question_list) {
        //you can use the question_list from here

    }
}
public类MainActivity扩展ActionBarActivity实现回调{
私有文件阅读器\u异步文件阅读器\u异步;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//当您想阅读问题..时调用asynctask调用asynctask代码
fileReader\u async=新fileReader\u async(getApplicationContext(),this);
fileReader_async.execute();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidMa中指定父活动时