希望你能幫助我。運行應用程式時它正在作業,但是當我再次運行它時它不作業并且沒有發現錯誤。我不知道為什么,但有時它作業,但有時它不作業。這是我的代碼....
公共類 filldisResult 擴展 AppCompatActivity {
ImageView imghome; TextView txtTotalQuesion, txtWrong, txtCorrect; int totalQuestion, correct, wrong; public static final String SCOREPREFERENCE = "shared_preference"; public static final String TOTALQUESTION = "total_score_preference"; public static final String WRONG = "wrong_preference"; public static final String CORRECT = "correct_preference"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_filldis_result); imghome = findViewById(R.id.img_homeS); txtCorrect = findViewById(R.id.txtCorrectQues); txtWrong = findViewById(R.id.txtWrongQues); txtTotalQuesion = findViewById(R.id.txtTotalQuestions); loadHighScore(); updateCorrectAnswer(correct); updateTotalScore(totalQuestion); updateWrongAnswer(wrong); imghome.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(filldisResult.this, filldisPlayScreen.class); startActivity(intent); finish(); } }); } private void updateTotalScore(int scoreToUpdate){ if(scoreToUpdate > totalQuestion){ totalQuestion = scoreToUpdate;SharedPreferences sharedPreferences = getSharedPreferences(SCOREPREFERENCE,MODE_PRIVATE); SharedPreferences.Editor 編輯器 = sharedPreferences.edit(); editor.putInt(TOTALQUESTION,totalQuestion); editor.apply(); } } 私人無效 updateCorrectAnswer(int scoreToUpdate){
if(scoreToUpdate > correct){ correct = scoreToUpdate; SharedPreferences sharedPreferences = getSharedPreferences(SCOREPREFERENCE,MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt(CORRECT,correct); editor.apply(); } } enter code here private void updateWrongAnswer(int scoreToUpdate){ if(scoreToUpdate > wrong){ wrong = scoreToUpdate; SharedPreferences sharedPreferences = getSharedPreferences(SCOREPREFERENCE,MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt(WRONG,wrong); editor.apply(); } }私人無效負載高分(){
SharedPreferences sharedPreferencesScore = getSharedPreferences(SCOREPREFERENCE, MODE_PRIVATE); totalQuestion = sharedPreferencesScore.getInt(TOTALQUESTION, 0); Log.i(" Total Questions :", " " totalQuestion); txtTotalQuesion.setText("" totalQuestion); SharedPreferences sharedPreferencesCorrect = getSharedPreferences(SCOREPREFERENCE, MODE_PRIVATE); correct = sharedPreferencesCorrect.getInt(CORRECT, 0); Log.i("Correct Answer :", " " correct); txtCorrect.setText("" correct); SharedPreferences sharedPreferencesWrong = getSharedPreferences(SCOREPREFERENCE, MODE_PRIVATE); wrong = sharedPreferencesWrong.getInt(WRONG, 0); Log.i("Wrong Answer :", " " wrong); txtWrong.setText("" wrong); } }
我的xml...
<線性布局
android:background="@drawable/background"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".filldisResult"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textColor="#fff"
android:textSize="35sp"
android:textAlignment="center"
android:textStyle="bold"
android:text="Score List"
android:layout_marginTop="8dp"
android:id="@ id/txtScoreText"/>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="120dp"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp">
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#fff"
android:textSize="30sp"
android:textAlignment="center"
android:textStyle="bold"
android:text="Total Questions :"
android:padding="10dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="end">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#fff"
android:textSize="30sp"
android:textAlignment="center"
android:textStyle="bold"
android:text="00"
android:id="@ id/txtTotalQuestions"
android:padding="10dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#fff"
android:textSize="30sp"
android:textAlignment="center"
android:textStyle="bold"
android:text="Score :"
android:padding="10dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="end">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#fff"
android:textSize="30sp"
android:textAlignment="center"
android:textStyle="bold"
android:text="00"
android:id="@ id/txtCorrectQues"
android:padding="10dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#fff"
android:textSize="30sp"
android:textAlignment="center"
android:textStyle="bold"
android:text="Wrong Answer :"
android:padding="10dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="end">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#fff"
android:textSize="30sp"
android:textAlignment="center"
android:textStyle="bold"
android:text="00"
android:id="@ id/txtWrongQues"
android:padding="10dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:padding="10dp"
android:layout_marginBottom="30dp">
<ImageView
android:background="@drawable/button_bg_for_settings_and_score"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="24dp"
android:id="@ id/img_homeS"
android:padding="10dp"
android:layout_marginBottom="24dp"
app:srcCompat="@drawable/home"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_weight="1"/>
</androidx.constraintlayout.widget.ConstraintLayout>
uj5u.com熱心網友回復:
commit()你editor之前做的apply()
if(scoreToUpdate > correct){
correct = scoreToUpdate;
SharedPreferences sharedPreferences = getSharedPreferences(SCOREPREFERENCE,MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(CORRECT,correct);
editor.commit(); //insert here
editor.apply();
}
private void updateWrongAnswer(int scoreToUpdate){
if(scoreToUpdate > wrong) {
wrong = scoreToUpdate;
SharedPreferences sharedPreferences = getSharedPreferences(SCOREPREFERENCE,MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(WRONG,wrong);
editor.commit(); //insert here
editor.apply();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/405917.html
標籤:
