问题
I have problems with asyncTask in android programming. I am trying to load a text file in a background thread which is asyncTask. First of all, in my application I send a variable from a class which is controlled by an array-list which is later controlled by a button in a xml-file to the asyncTask class because it chooses an index throug a spinner. When it comes to background thread class it fails since I do not know that part very well. If something was unclear please ask since I am beginner in android programming. Any help is appreciated!
Here is the code for sending a variable:
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){
fileReader_async=new FileReader_async(getApplicationContext(), this);
fileReader_async.execute("hogskoleprovet.txt");
}
else {
if (index == 1) {
fileReader_async = new FileReader_async(getApplicationContext(), this);
fileReader_async.execute("hogskoleprovet.txt");
} else if (index == 1) {
fileReader_async = new FileReader_async(getApplicationContext(), this);
fileReader_async.execute("hogskoleprovet.txt");
} else if (index == 2) {
fileReader_async = new FileReader_async(getApplicationContext(), this);
fileReader_async.execute("hogskoleprovet.txt");
}
}
setNewQuestion();
}
Here is the async-Class:
package com.example.arnpet.ultimatehogskoleprovet;
import android.content.Context;
import android.os.AsyncTask;
import com.example.arnpet.ultimatehogskoleprovet.Question;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.security.auth.callback.Callback;
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("hogskoleprovet");
} 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);
}
public interface Callback {
public void notify_result(List<Question> question_list);
}
public int getQuestionsLeft() {
return mQuestions.size();
}
public Question getRandomQuestion() {
Random random = new Random();
int index = random.nextInt(mQuestions.size());
Question newQuestion = mQuestions.get(index);
mQuestions.remove(index);
return newQuestion;
}
}
Here is the error-log:
05-02 09:04:28.823 29549-29549/com.example.arnpet.ultimatehogskoleprovet E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.arnpet.ultimatehogskoleprovet, PID: 29549
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3969)
at android.view.View.performClick(View.java:4637)
at android.view.View$PerformClick.run(View.java:19422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5479)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3964)
android.view.View.performClick(View.java:4637)
at android.view.View$PerformClick.run(View.java:19422)
来源:https://stackoverflow.com/questions/29999620/problems-with-running-asynctask-in-android-studio-java