Problems with Static When Calling an AsyncTask

I have problems with an invoking to an asyncTask. It says that I cannot use static method non-excute method ‘excute(Params…)’ cannot be referenced from a static context. I have browsed through this site and other, I cannot find an useable solution to my problems. Any help is appreciated! If something was unclear please ask.

Here is where the invoking is taken place:

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_list) {
        //you can use the question_list from here
    }
}

And here is where the receiver gets the invoking:

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 mQuestions;
    public QuestionBox(Context context,Callback callback)
    {
        mQuestions = new ArrayList();
        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_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;
    }
}
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Problems with Static When Calling an AsyncTask

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录