我在创建一个在后台运行的方法时遇到了一些问题。我正在使用Android studio java
,并希望创建一个在array-list
中加载文本文件的方法。我试图在与活动类不同的类中的公共方法中执行此操作。当我运行应用程序时,我遇到问题表明主线程有问题,所以我想制作一个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();
}
}
我想把这段代码作为asynctask,但我不知道怎么做。
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();
}
答案 0 :(得分:0)
尝试修改您的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();
}
答案 1 :(得分:0)
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);
}
}
回调接口,在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
}
}