首页 > 解决方案 > Android:java.lang.NullPointerException:尝试调用接口方法'boolean android.content.SharedPreferences.contains(java.lang.String)'

问题描述

我在这里遇到错误。我设计了一个应用程序,其中涉及用户注册页面和登录页面。用户注册成功,数据也被添加并发布到 SQLITE。

但是,在登录期间,我遇到了一个问题。一旦用户登录,我必须打开一个新页面。但是,由于 sharedpreference,我面临的错误是空指针异常。

这是我的登录页面,点击login按钮后,我遇到了错误。

在此处输入图像描述

com.example.dell.digitalwallet E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dell.digitalwallet, PID: 3206
java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.content.SharedPreferences.contains(java.lang.String)' on a null object reference
    at com.example.dell.digitalwallet.Login$1.onClick(Login.java:56)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25881)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6649)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)

06-02 12:39:50.866 3206-3206/com.example.dell.digitalwallet I/Process:发送信号。PID:3206 SIG:9

我创建了以下类:

Login.java
    package com.example.dell.digitalwallet;


import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class Login extends AppCompatActivity {
    private static TextView username;
    private static TextView password;
    private static EditText User_Name;
    private static EditText User_Password;
    private static Button login_btn, Registration, passwd_btn, Add_Person;
    private SharedPreferences sharedPreferences;


    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Click_Button();
    }

    public  void Click_Button() {
        username = (TextView)findViewById(R.id.User_Name);
        password = (TextView)findViewById(R.id.Password);
        Registration = (Button) findViewById(R.id.button_registration);
        login_btn = (Button)findViewById(R.id.button_login);
        passwd_btn=(Button) findViewById(R.id.button_password);
        User_Name = (EditText) findViewById(R.id.Name);
        User_Password = (EditText) findViewById(R.id.User_Password);
        Add_Person = (Button) findViewById(R.id.add_person);


        login_btn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String username = User_Name.getText().toString();
                        String password = User_Password.getText().toString();

                        // Validate if username, password is filled
                        if(username.trim().length() > 0 && password.trim().length() > 0){
                            String uName = null;
                            String uPassword =null;
                            if (sharedPreferences.contains(username))
                            {
                                uName = sharedPreferences.getString(username,"");

                            }

                            if (sharedPreferences.contains(password))
                            {
                                uPassword = sharedPreferences.getString(password, "");

                            }


                            if (username.equals(uName) && password.equals(uPassword)){

                            Toast.makeText(Login.this,"User and Password is correct",
                                    Toast.LENGTH_SHORT).show();

                            //Starting the User's Home Page
                                Add_Person.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        startActivity(new Intent(Login.this, Parson_Details.class));
                                    }
                                });


                        } else {
                            Toast.makeText(Login.this,"User and Password is not correct",
                                    Toast.LENGTH_SHORT).show();

                            }
                          }


                    }
                }

        );

        Registration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Login.this, Users.class));
            }
        });

       passwd_btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               startActivity(new Intent(Login.this, Password.class));
           }
       }

       );

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

  }

Parson_Details.java

package com.example.dell.digitalwallet;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.support.annotation.Nullable;

import android.telephony.PhoneNumberFormattingTextWatcher;
import android.text.TextUtils;
import android.util.Log;
import android.util.Patterns;
import android.view.View;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;

import java.lang.String;


public class Parson_Details extends AppCompatActivity {


    private String FirstName;
    private String MiddleName;
    private String LastName;
    private String Address;
    private int PhoneNumber;
    private int TotalCrBal;
    private int TotalDbBal;

    private static TextView First_Name;
    private static TextView Middle_Name;
    private static TextView Last_Name;
    private static TextView Address_Person;
    private static TextView Phone_Number;
    private static TextView Total_Cr_Bal;
    private static TextView Total_Db_Bal;
    private static Button Save;

    private static EditText First_N;
    private static EditText Middle_N;
    private static EditText Last_N;
    private static EditText Addr;
    private static EditText Phone_N;
    private static EditText Cr_Bal;
    private static EditText Db_Bal;
    private static Button save;

    public String getFirst_Name() {return FirstName;}
    public void setFirst_Name(String FirstName) {this.FirstName = FirstName;}

    public String getMiddle_Name() {return MiddleName;}
    public void setMiddle_Name(String Middle_Name) {this.MiddleName = Middle_Name;}

    public String getLast_Name() {return LastName;}
    public void setLast_Name(String Last_Name) {this.LastName = Last_Name;}

    public String getAddress() {return Address;}
    public void setAddress(String Address) {this.Address = Address;}

    public int getPhone_Number() {return PhoneNumber;}
    public void setPhone_Number(int Phone_Number) {this.PhoneNumber = Phone_Number;}

    public int getTotal_Cr_Bal() {return TotalCrBal;}
    public void setTotal_Cr_Bal(int TotalCrBal) {this.TotalCrBal = TotalCrBal;}

    public int getTotalDbBal() {return TotalDbBal;}
    public void setTotalDbBal(int TotalDbBal) {this.TotalDbBal = TotalDbBal;}


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homescreen);
        getSupportActionBar().hide();
        Click_Save_Button();

    }
        public  void Click_Save_Button() {

            First_Name = (TextView) findViewById(R.id.First_Name);
            Middle_Name = (TextView)findViewById(R.id.Middle_Name);
            Last_Name = (TextView) findViewById(R.id.Last_Name);
            Address_Person= (TextView) findViewById(R.id.Address);
            Phone_Number=(TextView) findViewById(R.id.Phone_Number);
            Total_Cr_Bal = (TextView) findViewById(R.id.Total_Cr_Bal);
            Total_Db_Bal = (TextView) findViewById(R.id.Total_Db_Bal);
            Save = (Button) findViewById(R.id.save);

            First_N = (EditText) findViewById(R.id.First_N);
            Middle_N = (EditText)findViewById(R.id.Middle_N);
            Last_N = (EditText) findViewById(R.id.Last_N);
            Addr = (EditText)findViewById(R.id.Addr);
            Phone_N=(EditText) findViewById(R.id.Phone_N);
            Cr_Bal = (EditText) findViewById(R.id.Cr_Bal);
            Db_Bal = (EditText) findViewById(R.id.Db_Bal);
            Save = (Button) findViewById(R.id.save);


    }
}

Parson_Details.java 不完整。单击登录按钮后,我需要首先到达添加用户的主屏幕,在这里,必须启动一个名为 Add_Person 的新活动。

添加人物布局

在此处输入图像描述

我到底做错了什么?

标签: androidandroid-layoutandroid-studio

解决方案


以上Click_Button()方法,过去那个

sharedPrefernces = PreferenceManager.getDefaultSharedPreferences(this);

推荐阅读