In my project I have two activities:
Redeem Activity
Block Activity
In My
Redeem Activity
Well in this app what i want to do is whenever a user click Redeem Button today's date value will be stored in shared preferences.Now whenever he the user tries to launch this app again Redeem Activity will compare today's date with shared preference date value.
If today's date which i stored in variable a == to shared preference value
Then i want to send person to Block activity so he can't access redeem activity till next day
Now According to my code I got successful in saving date value in shared preference
but problem is i even wrote a comparing function(checkstatus()) below is the code
private void checkStatus(){
//comparison and locking activity
SharedPreferences sharedPref = getSharedPreferences("log",Context.MODE_PRIVATE);
String saveddate = sharedPref.getString("date","");
if(a.equals(saveddate)){
Intent Intent = new Intent(Redeem.this,Block.class);
startActivity(Intent);
}
}
but on launching app again even though values of date are same still Redeem Activity is opening Intent is not working and I don't know why.
I am getting this errors now:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.packagenamek/com.packagename.Redeem}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
Below is my Code Redeem.java
package com.packagename;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Date;
public class Redeem extends AppCompatActivity {
TextView textView,displaysp;
Button redeem,displaydate;
Date date;
String a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_redeem);
checkStatus();
textView = (TextView) findViewById(R.id.label);
redeem = (Button) findViewById(R.id.redeem);
date = new Date(System.currentTimeMillis());
a = date.toString();
textView.setText(a);
//let see saved value
displaysp = (TextView) findViewById(R.id.label1);
redeem = (Button) findViewById(R.id.btndisplay);
}
public void saveInfo(View view){
//Writing date into Shared Preference
SharedPreferences sharedPref =getSharedPreferences("log", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("date",a);
editor.apply();
Toast.makeText(this,"Redeem Request In Process",Toast.LENGTH_LONG).show();
}
public void displayDate(View view){
SharedPreferences sharedPref = getSharedPreferences("log",Context.MODE_PRIVATE);
String saveddate = sharedPref.getString("date","");
displaysp.setText(saveddate);
}
private void checkStatus(){
//comparison and locking activity
SharedPreferences sharedPref = getSharedPreferences("log",Context.MODE_PRIVATE);
String saveddate = sharedPref.getString("date","");
if(a.equals(saveddate)){
Intent Intent = new Intent(Redeem.this,Block.class);
startActivity(Intent);
}
}
}
Redeem.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.packagename.Redeem">
<TextView
android:id="@+id/label"
android:layout_width="115dp"
android:layout_height="25dp"
android:text="TextView"
android:textSize="20dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginEnd="130dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="196dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="134dp" />
<TextView
android:id="@+id/label1"
android:layout_width="115dp"
android:layout_height="25dp"
android:text="Display"
android:textSize="20dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginEnd="130dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="352dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="121dp" />
<Button
android:id="@+id/btnredeem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="95dp"
android:onClick="saveInfo"
android:text="redeem"
app:layout_constraintBottom_toTopOf="@+id/label"
app:layout_constraintLeft_toLeftOf="@+id/label"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1" />
<Button
android:id="@+id/btndisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="displayDate"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="9dp"
android:layout_marginTop="25dp"
app:layout_constraintTop_toBottomOf="@+id/label"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/label"
android:layout_marginLeft="0dp" />
</android.support.constraint.ConstraintLayout>