Session implementation does not work

I'm designing a login form to implement the session. After implementing the session, the following error occurred. I can't understand why

Login.java file

package com.example.android.bet;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message; 
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Login extends AppCompatActivity {

private EditText username;
private EditText password;
private Button login;
Context context;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String User = "userKey";
public static final String Pass = "passKey";
String uid,upass;
SharedPreferences sharedpreferences;
SharedPreferences.Editor editor;
private final static String  restURL="XXXXXXXXXX";
Message msg = new Message();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    context=this;
    setupVariables();

}

Handler handler = new Handler(new Handler.Callback() {

    @Override
    public boolean handleMessage(Message msg) {
        if(msg.arg1==1)
        {
            Toast.makeText(context, "email id or password invalid", Toast.LENGTH_SHORT).show();

            //Print Toast or open dialog
        }
        return false;
    }
});

public void authenticateLogin(View view)
{

    new Thread()
    {
        public void run()
        {

            String apiurl=restURL+"XXXXXXX"+username.getText().toString()+"&Password="+password.getText().toString();
            Log.v("URL Generated", apiurl);
            String betJsonStr=null;
            try {

                HttpURLConnection urlConnection = null;
                BufferedReader reader = null;

                try {
                    URL url = new URL(apiurl);

                    urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setRequestMethod("GET");
                    urlConnection.connect();

                    InputStream inputStream = urlConnection.getInputStream();

                    StringBuffer buffer = new StringBuffer();

                    reader = new BufferedReader(new InputStreamReader(inputStream));

                    String line;
                    while ((line = reader.readLine()) != null) {
                        buffer.append(line + "\n");
                    }
                    betJsonStr = buffer.toString();
                    Log.v("json",betJsonStr);
                } catch (IOException e) {
                    Log.e("Didn't get data", "Error", e);
                }

                try {
                    JSONObject Object = new JSONObject(betJsonStr);
                    int t = Integer.parseInt(Object.optString("Account").toString());
                    String mt=""+t;
                    Log.v("Value of t",mt);
                    t=1;
                    //just dummy entry as api is not working properly to check whether login is working or not
                    if(t==0)
                    {
                        Log.v("incoming ","yes");
                        msg.arg1=1;
                        handler.sendMessage(msg);



                    }
                    else
                    {
                        editor = sharedpreferences.edit();
                        editor.putString(User,uid);
                        editor.putString(Pass,upass);
                        editor.commit();
                        Intent menu=new Intent(context,MainActivity.class);
                        startActivity(menu);

                    }



                }

                catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            catch (Exception e) {
                Log.e("Exception arised", "error", e);
            }

        }
    }.start();
}

private void setupVariables()
{


    username=(EditText) findViewById(R.id.username);
    password=(EditText) findViewById(R.id.password);
    login=(Button) findViewById(R.id.login);

    uid=username.getText().toString();
    upass=password.getText().toString();
}
}

activity_ Login.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100"
tools:context="com.example.android.bet.Login">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="40"
    android:background="#7ABA3A"
    android:orientation="vertical">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:layout_gravity="center|center_horizontal"
        android:src="@drawable/account"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center|center_horizontal"
        android:layout_weight="1"
        android:text="logo"
        android:textSize="18sp"
        android:textColor="#f1f1f1"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="50"
    android:paddingTop="40dp"
    android:orientation="vertical">


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="5dp"
        android:ems="10"
        android:layout_gravity="center|center_horizontal"
        android:id="@+id/username"
        android:hint="Email Id"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="10dp"
        android:ems="10"
        android:layout_gravity="center|center_horizontal"
        android:id="@+id/password"
        android:hint="Password"/>

    <Button
        android:background="#7ABA3A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LOGIN"
        android:layout_marginBottom="10dp"
        android:textColor="#f1f1f1"
        android:layout_gravity="center|center_horizontal"
        android:id="@+id/login"
        android:onClick="authenticateLogin"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="10"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TradeCoreTech"
        android:textColor="#7ABA3A"
        android:layout_gravity="center|center_horizontal"/>
</LinearLayout>


</LinearLayout>

Wrong monosodium glutamate

03-30 12:42:22.414 325-20365/? V/URL Generated: XXXXXXXXXXXXX
03-30 12:42:23.064 325-20365/? V/json: "API data"
03-30 12:42:23.074 325-20365/? V/Value of t: 0
03-30 12:42:23.074 325-20365/? E/Exception arised: error
03-30 12:42:23.074 325-20365/? E/Exception arised: java.lang.NullPointerException

resolvent:

It seems that you didn't create the SharedPreferences object. You need something similar

 sharedpreferences = new SharedPreferences();

Before editor = SharedPreferences. Edit();

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>