2015-05-09 60 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