2015-05-09 54 views
1

我有关于方法访问其他方法的问题,因为日志猫指出空指针的问题。我试图给一个方法构造一个对象,我尝试使用其他方法。任何帮助表示赞赏!如果有什么不清楚的地方,请问!在android中的访问方法的问题java

这里就是日志的猫说,问题出在哪里代码:

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 
+0

请包含的代码'QuestionBox.getRandomQuestion' – Eran

回答

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指定在这个异常被抛出:

- 如果根据您的编辑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; 
} 
+0

我将如何解决呢? – Bamse

+0

是不是因为我发送了一个来自微调控制器的数值,它在数组列表中是0? – Bamse

+0

@Bamse问题是当'mQuestions'为空时,你的代码调用'getRandomQuestion()'是否有效。如果是,只需向该方法添加验证即可。如果不是,请找出它发生的原因。 – Eran