在我的项目中,我有两项活动:
兑换活动
阻止活动
在我的
兑换活动
在这个应用程序中,我想要做的是每当用户点击兑换按钮时,今天的日期值将存储在共享首选项中。现在,只要用户再次尝试启动此应用兑换活动,就会将今天的日期与共享偏好日期值进行比较。
如果今天的日期,我存储在变量 a == 共享偏好值
中然后我想派人去阻止活动,这样他就无法访问兑换活动直到第二天
现在根据我的代码,我成功地在共享偏好中保存了日期值
但问题是我甚至写了比较函数( checkstatus())下面是代码
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);
}
}
但是,即使日期值相同再次启动应用仍然兑换活动正在打开意图无效,我不会知道为什么。
我现在收到这个错误:
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.packagenamek / com.packagename.Redeem}: java.lang.NullPointerException:尝试调用虚方法 空对象上的'boolean java.lang.String.equals(java.lang.Object)' 参考
以下是我的代码 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>
答案 0 :(得分:0)
初始化字符串变量checkStatus()
a
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_redeem);
textView = (TextView) findViewById(R.id.label);
redeem = (Button) findViewById(R.id.redeem);
date = new Date(System.currentTimeMillis());
a = date.toString();
//checkStatus called after initialising a
checkStatus();
textView.setText(a);
//let see saved value
displaysp = (TextView) findViewById(R.id.label1);
redeem = (Button) findViewById(R.id.btndisplay);
}
private void checkStatus(){
//comparison and locking activity
SharedPreferences sharedPref = getSharedPreferences("log",Context.MODE_PRIVATE);
String saveddate = sharedPref.getString("date","");
if(a.contentEquals(saveddate)){
Intent Intent = new Intent(Redeem.this,Block.class);
startActivity(Intent);
}
}