在android java中访问方法的问题

时间:2015-05-09 18:12:39

标签: java android

我在访问其他方法时遇到问题,因为log-cat表示空指针有问题。我试过一个方法,一个对象的构造函数,我尝试用于其他方法。任何帮助表示赞赏!如果有什么不清楚请问!

以下是log-cats说明问题所在的代码:

public class gameAction extends ActionBarActivity implements QuestionBox.Callback{

private QuestionBox mQuestionBox;
private Question mCurrentQuestion;
private Context context;
private Callback callback;

@Override
public void notify_result(List<Question> question_list) {}

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(getApplicationContext(), this);
        mQuestionBox.execute("hogskoleprovet.txt");
        setNewQuestion();
    } else {
        if (index == 1 ) {
            mQuestionBox =new QuestionBox(getApplicationContext(), this);
            mQuestionBox.execute("hogskoleprovet.txt");
            setNewQuestion();
        } else if (index == 1) {
            mQuestionBox =new QuestionBox(getApplicationContext(), this);
            mQuestionBox.execute("hogskoleprovet.txt");
            setNewQuestion();
        } else if (index == 2) {
            mQuestionBox =new QuestionBox(getApplicationContext(), this);
            mQuestionBox.execute("hogskoleprovet.txt");
        }
    }
}

public void setNewQuestion() {
    mCurrentQuestion = mQuestionBox.getRandomQuestion();

    TextView questionTextView = (TextView) findViewById(R.id.questionTextView);
    questionTextView.setText(mCurrentQuestion.getQuestion());

    Button buttonOne = (Button) findViewById(R.id.buttonOne);
    buttonOne.setText(mCurrentQuestion.getOptionOne());

    Button buttonTwo = (Button) findViewById(R.id.buttonTwo);
    buttonTwo.setText(mCurrentQuestion.getOptionTwo());

    Button buttonThree = (Button) findViewById(R.id.buttonThree);
    buttonThree.setText(mCurrentQuestion.getOptionThree());

    Button buttonFour = (Button) findViewById(R.id.buttonFour);
    buttonFour.setText(mCurrentQuestion.getOptionFour());

    Button buttonNew = (Button) findViewById(R.id.buttonNew);

    buttonOne.setEnabled(true);
    buttonTwo.setEnabled(true);
    buttonThree.setEnabled(true);
    buttonFour.setEnabled(true);

    buttonNew.setVisibility(View.INVISIBLE);

    buttonOne.setText(mCurrentQuestion.getOptionOne());
    buttonTwo.setText(mCurrentQuestion.getOptionTwo());
    buttonThree.setText(mCurrentQuestion.getOptionThree());
    buttonFour.setText(mCurrentQuestion.getOptionFour());
}

public void quitTheGame(View v) {
    Intent intent = new Intent (this, MainActivity.class);
    Button butttonQuit =  (Button) findViewById(R.id.buttonFive);
    startActivity(intent);
}

public void answerClick(View V) {
    Button answerButton = (Button)V;
    Button buttonOne = (Button) findViewById(R.id.buttonOne);
    buttonOne.setText(mCurrentQuestion.getOptionOne());

    Button buttonTwo = (Button) findViewById(R.id.buttonTwo);
    buttonTwo.setText(mCurrentQuestion.getOptionTwo());

    Button buttonThree = (Button) findViewById(R.id.buttonThree);
    buttonThree.setText(mCurrentQuestion.getOptionThree());

    Button buttonFour = (Button) findViewById(R.id.buttonFour);
    buttonFour.setText(mCurrentQuestion.getOptionFour());

    Button buttonNew = (Button) findViewById(R.id.buttonNew);

    buttonOne.setEnabled(false);
    buttonTwo.setEnabled(false);
    buttonThree.setEnabled(false);
    buttonFour.setEnabled(false);
    buttonNew.setVisibility(View.VISIBLE);
}

public void newClick(View v) {
    if(mQuestionBox.getQuestionsLeft()>0){
        setNewQuestion();
    } else {
        Context context = getApplicationContext();
        String text = "Slut på frågor!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
}
}

Log cat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.arnpet.ultimatehogskoleprovet/com.example.arnpet.ultimatehogskoleprovet.gameAction}: java.lang.IllegalArgumentException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalArgumentException
        at java.util.Random.nextInt(Random.java:185)
        at com.example.arnpet.ultimatehogskoleprovet.QuestionBox.getRandomQuestion(QuestionBox.java:111)
        at com.example.arnpet.ultimatehogskoleprovet.gameAction.setNewQuestion(gameAction.java:91)
        at com.example.arnpet.ultimatehogskoleprovet.gameAction.onCreate(gameAction.java:52)
        at android.app.Activity.performCreate(Activity.java:5104)
        at      a

1 个答案:

答案 0 :(得分:1)

Caused by: java.lang.IllegalArgumentException
        at java.util.Random.nextInt(Random.java:185)
        at com.example.arnpet.ultimatehogskoleprovet.QuestionBox.getRandomQuestion(QuestionBox.java:111)

在调用IllegalArgumentException时看起来有Random.nextInt(n)个例外。 Javadoc指定何时抛出此异常:

  

IllegalArgumentException - 如果n不是正数

根据您的修改:

int index = random.nextInt(mQuestions.size());

我说mQuestions.size()0

我不确定为什么您的列表为空,但您可以通过在调用random.nextInt之前检查列表的大小来避免此异常。在这种情况下,您决定该怎么做。您可以返回null或抛出异常。

public Question getRandomQuestion() {
    if (mQuestions.size() < 1)
        return null;
    Random random = new Random();
    int index = random.nextInt(mQuestions.size());
    Question newQuestion = mQuestions.get(index);
    mQuestions.remove(index);
    return newQuestion;
}