Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中使用共享首选项进行日期比较不起作用_Java_Android - Fatal编程技术网

Java 在android中使用共享首选项进行日期比较不起作用

Java 在android中使用共享首选项进行日期比较不起作用,java,android,Java,Android,在我的项目中,我有两项活动: 赎回活动 阻止活动 在我的 赎回活动 在这个应用程序中,我想做的是,每当用户单击赎回按钮时,今天的日期值将存储在共享首选项中。现在,每当用户再次尝试启动这个应用程序时,赎回活动都会将今天的日期与共享首选项进行比较日期值 如果我存储在变量a==to共享首选项值中的今天的日期 然后,我想将此人发送到阻止活动,以便他在第二天之前无法访问赎回活动 现在,根据我的代码,我成功地将日期值保存在共享首选项中 但问题是我甚至编写了一个比较函数(checkstatus()),下面是代

在我的项目中,我有两项活动:

  • 赎回活动

  • 阻止活动

  • 在我的

    赎回活动

    在这个应用程序中,我想做的是,每当用户单击赎回按钮时,今天的日期值将存储在共享首选项中。现在,每当用户再次尝试启动这个应用程序时,赎回活动都会将今天的日期共享首选项进行比较日期

    如果我存储在变量a==to共享首选项值中的今天的日期

    然后,我想将此人发送到阻止活动,以便他在第二天之前无法访问赎回活动

    现在,根据我的代码,我成功地将日期值保存在共享首选项中

    但问题是我甚至编写了一个比较函数(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.redeme}: java.lang.NullPointerException:尝试调用虚拟方法 空对象上的“boolean java.lang.String.equals(java.lang.Object)” 参考文献

    下面是我的代码赎回.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);
            }
        }
    }
    
    救赎.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>
    

    初始化字符串变量
    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);
        }
    }
    

    checkStatus()。。。a=…
    显然
    a
    是null@Selvin兄弟,我读到了,但我没有找到解决办法。问题是日期比较不起作用。Move
    checkStatus()…textView.setText(a)下的code>谢谢你,顺便问一下,你也能解决这个错误吗?我尝试过我的空指针错误也消失了,但意图仍然不起作用,我不知道为什么。赎回活动仍然打开,即使日期值sameThanks很多兄弟,我试过了,错误消失了,但日期比较是checkStatus()函数不起作用。即使两个值相等,仍会赎回活动期初。它应该触发阻止活动的意图,但它没有发生。我不知道为什么可以使用
    contentEquals()
    代替
    equals()
    为了比较Strings,我也尝试过,但意图不起作用,我不知道为什么。即使两个日期值相同,赎回活动仍然打开