我是android編程的初學者。如果條件滿足,如何去android的另一個活動?
我在製作一個測驗應用程序,如果玩家正確回答,我可以提高分數。如果玩家回答錯誤,我想在玩家最後得分時顯示屏幕上的遊戲。我用下面的代碼來更新分數和級別,每個正確答案,並用一個錯誤的答案開始GameOverActivity.java:
boolean isCorrect(int answerGiven) {
if (answerGiven == correctAnswer) {
Toast.makeText(getApplicationContext(), "Well done!",
Toast.LENGTH_LONG).show();
correctTrueOrFalse = true;
} else {
correctTrueOrFalse = false;
}
return correctTrueOrFalse;
}
void updateScoreAndLevel(int answerGiven) {
if (isCorrect(answerGiven)) {
for (int i = 1; i <= currentLevel; i++) {
currentScore = currentScore + i;
}
currentLevel++;
}
else {
Intent k = new Intent (GameActivity.this, GameOverActivity.class); //#######
startActivity(k);
}
}
但每當我運行這個程序,分數和級別,每個正確的答案已成功更新,但只要我給出了錯誤的答案,該應用程序就會崩潰,並且無法啓動GameOverActivity.class(遊戲屏幕上的代碼)。
我的GameOverActivity文件似乎是完全正確的,我覺得問題在######
標記的行。請幫我弄清楚這個問題。
編輯:我的清單文件:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
android:screenOrientation="portrait"
</activity>
<activity android:name=".GameActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
android:screenOrientation="portrait"
</activity>
</application>
是的,我看到我的GameOverActivity文件清單不註冊。我認爲android studio自動註冊清單中的文件。那麼如何註冊呢?請記住我在GameActivity和GameOverActivity文件中都有意圖,因此可能必須相應地更改清單。
你能分享你的日誌? –
和GameOverAcitivty,似乎錯誤是 – Chol
你的邏輯它的確定,需要看日誌 –