调用asyncTask时静态问题

时间:2015-05-09 13:43:00

标签: java android android-asynctask

我在调用asyncTask时遇到问题。它说我不能使用静态方法非执行方法'excute(Params ...)'不能从静态上下文中引用。我浏览了这个网站和其他网站,我找不到一个可用的解决方案来解决我的问题。任何帮助表示赞赏!如果有什么不清楚请问。

以下是调用的地方:

public class gameAction extends ActionBarActivity
implements QuestionBox.Callback {
    private QuestionBox mQuestionBox;
    private Question mCurrentQuestion;
    private Context context;
    private Callback callback;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_action);
        //Kod som kollar vad som skickades med när aktiviteten startades
        Intent callingIntent = getIntent();
        int index = callingIntent.getIntExtra("INDEX",0);

        //Bestäm filnamn beroende på vad som skickades med
        setNewQuestion();

        if(index==0){
            QuestionBox =new QuestionBox(getApplicationContext(), this);
            QuestionBox.execute("hogskoleprovet.txt");
        }
        else {
            if (index == 1 ) {
                QuestionBox =new QuestionBox(getApplicationContext(), this);
                QuestionBox.execute("hogskoleprovet.txt");
            } else if (index == 1) {
                QuestionBox =new QuestionBox();
                QuestionBox.execute("hogskoleprovet.txt");
            } else if (index == 2) {
                QuestionBox =new QuestionBox();
                QuestionBox.execute("hogskoleprovet.txt");
            }
        }
    }

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

这是接收者获取调用的地方:

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 QuestionBox extends AsyncTask {
    private Context context;
    private Callback callback;
    private List<Question> mQuestions;
    public QuestionBox(Context context,Callback callback)
    {
        mQuestions = new ArrayList<Question>();
        this.callback=callback;
        this.context= context;

    }
    public  Callback getCallback(){
        return 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));
        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);
        ((gameAction) context).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;
    }
}

2 个答案:

答案 0 :(得分:0)

您的自定义任务不仅应延伸android.os.AsyncTask<Params, Progress, Result> AsyncTask,而且根据您的情况,QuestionBox应该延伸AsyncTask<String, Void, Void>

有关详细信息,请查看此链接http://developer.android.com/reference/android/os/AsyncTask.html

答案 1 :(得分:0)

QuestionBox是您的AsyncTask子类的名称,在您的Activity中没有名为QuestionBox的成员名为QuestionBox。您虽然宣布QuestionBox mQuestionBox为班级成员。变化

QuestionBox =new QuestionBox(getApplicationContext(), this);
QuestionBox.execute("hogskoleprovet.txt");

mQuestionBox =new QuestionBox(getApplicationContext(), this);
mQuestionBox.execute("hogskoleprovet.txt");