我正在創建一種適用於練習的大腦教練應用程序。有兩個數字被添加或減少,我們需要猜測結果。有四個按鈕,一個是正確的值,另外三個是隨機數。我有一個函數setQuestion()來完成這一切。 當我按下其中一些按鈕時,它會檢查數字是否正確並更新分數。它應該調用函數setQuestion(),以便它創建新的值,但由於某種原因,我點擊一個按鈕後,應用程序崩潰。從另一個功能調用某個功能後,應用程序停止工作
setQuestion函數起作用,因爲它在應用程序打開時運行。 這是這兩個函數的代碼。
public void setQuestion(){
int firstNumber = randomNumberGenerator.nextInt(101);
int secondNumber = randomNumberGenerator.nextInt(101);
int operation = randomNumberGenerator.nextInt(2);
TextView firstNumberTextView = (TextView) findViewById(R.id.firstNumber);
TextView secondNumberTextView = (TextView) findViewById(R.id.secondNumber);
TextView operationTextView = (TextView) findViewById(R.id.operation);
if(operation == 0){
sum = firstNumber + secondNumber;
operationTextView.setText("+");
}
else{
while (secondNumber > firstNumber){
secondNumber = randomNumberGenerator.nextInt(101);
}
operationTextView.setText("-");
sum = firstNumber - secondNumber;
}
firstNumberTextView.setText(String.valueOf(firstNumber));
secondNumberTextView.setText(String.valueOf(secondNumber));
int operation1 = randomNumberGenerator.nextInt(2);
int operation2 = randomNumberGenerator.nextInt(2);
int operation3 = randomNumberGenerator.nextInt(2);
int answer1 = 0;
int answer2 = 0;
int answer3 = 0;
switch (operation1){
case 0:
answer1 = sum + randomNumberGenerator.nextInt(9) +1 +1;
break;
case 1:
int temp = randomNumberGenerator.nextInt(9) +1;
if(sum > temp){
answer1 = sum - temp;
}
else{
answer1 = temp;
}
}
switch (operation2){
case 0:
answer2 = sum + randomNumberGenerator.nextInt(9) +1;
break;
case 1:
int temp = randomNumberGenerator.nextInt(9) +1;
if(sum > temp){
answer2 = sum - temp;
}
else{
answer2 = temp;
}
}
switch (operation3){
case 0:
answer3 = sum + randomNumberGenerator.nextInt(9) +1;
break;
case 1:
int temp = randomNumberGenerator.nextInt(9) +1;
if(sum > temp){
answer3 = sum - temp;
}
else{
answer3 = temp;
}
}
buttonMap.add(0);
buttonMap.add(1);
buttonMap.add(2);
buttonMap.add(3);
Collections.shuffle(buttonMap);
int[] valArray = {sum, answer1, answer2, answer3};
for (int i = 0; i < buttonMap.size(); i++) {
int id = getResources().getIdentifier("b"+buttonMap.get(i), "id", getPackageName());
Button button = (Button) findViewById(id);
button.setText(String.valueOf(valArray[i]));
}
}
另:
public void answer(View view){
Button clickedButton = (Button) view;
String clickedValue = clickedButton.getText().toString();
int clickedValueInt = Integer.parseInt(clickedValue);
TextView allQuestion = (TextView) findViewById(R.id.totalQuestions);
numberOfQuestions = Integer.parseInt(allQuestion.getText().toString());
TextView correctQuestions = (TextView) findViewById(R.id.correctQuestions);
numberOfCorrectQuestions = Integer.parseInt(correctQuestions.getText().toString());
if(clickedValueInt == sum){
numberOfCorrectQuestions++;
correctQuestions.setText(String.valueOf(numberOfCorrectQuestions));
}
System.out.println(numberOfCorrectQuestions);
numberOfQuestions++;
allQuestion.setText(String.valueOf(numberOfQuestions));
setQuestion();
}
這裏是從logcat的文本。
02-26 02:34:19.965 305-305 /? E/lowmemorykiller:寫入/ proc/18289/oom_score_adj時出錯; errno = 22 02-26 02:34:19.987 850-6076 /? E/ReportTools:這不是測試版用戶版本 02-26 02:34:20.167 850-1922 /? E/HsmCoreServiceImpl:代碼中的onTransact爲:102 02-26 02:34:20.221 27755-27832 /? E/PackageLogInfoManager:checkPackageLogState,cr:[email protected],packageNames:null 02-26 02:34:20.327 18468-18486 /? E/linker:readlink('/ proc/self/fd/23')失敗:權限被拒絕[fd = 23] 02-26 02:34:20.328 18468-18486 /? E/linker:警告:無法獲得庫的實際路徑「/system/lib64/hw/gralloc.hi6250.so」。將使用給定的名稱。 02-26 02:34:20.335 18468-18486 /? E/linker:readlink('/ proc/self/fd/23')失敗:權限被拒絕[fd = 23] 02-26 02:34:20.335 18468-18486 /? E/linker:警告:無法爲庫「libion.so」獲取實際路徑。將使用給定的名稱。 02-26 02:34:20.461 18468-18468 /? E/TotemWeather:使用錯誤:getWidgetShowCityInfo返回myLocation 02-26 02:34:20.463 18468-18485 /? E/TotemWeather:使用錯誤:getWidgetShowCityInfo返回myLocation 02-26 02:34:20.522 9539-9608 /? E/HwLauncher:SettingsEx,沒有這樣的字段。 02-26 02:34:20.525 18468-18468 /? E/TotemWeather:使用錯誤:getWidgetShowCityInfo返回myLocation 02-26 02:34:20.929 1854-1854 /? E/RegisteredServicesCache:invalidateCache set mNeedToastTableFull
哇這是一個很大的功能 – user7568042
在我的防守中,我在5天前開始了Android和java。但我知道我應該做更小的功能,對吧? –