Issue
Hope you will help me. When run the app It was working, but then when, I run it again it is not working and theres no error to be found. I dont know why, but sometimes it work but sometimes it not working. Here is my code....
public class filldisResult extends 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 editor = sharedPreferences.edit(); editor.putInt(TOTALQUESTION,totalQuestion); editor.apply(); } } private void 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(); } }
private void loadHighScore() {
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); } }
my xml...
<LinearLayout
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>
Solution
commit()
your editor
before do 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();
}
}
Answered By - EAS
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.