gpt4 book ai didi

java - 在android java中访问方法的问题

转载 作者:行者123 更新时间:2023-11-30 02:10:43 26 4
gpt4 key购买 nike

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

这是日志猫指出问题所在的代码:

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();
}
}
}

日志猫:

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

最佳答案

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

看起来您在调用 Random.nextInt(n) 时遇到了 IllegalArgumentException 异常。 Javadoc 指定何时抛出此异常:

IllegalArgumentException - if n is not positive

基于您的编辑:

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;
}

关于java - 在android java中访问方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30143418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com