【问题标题】:Session implementation not working会话实现不起作用
【发布时间】:2016-03-30 15:55:28
【问题描述】:

我正在设计一个登录表单,我在其中实施会话。实施会话后,我收到以下错误。无法理解为什么会这样。

Login.java 文件

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 文件

<?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>

错误信息

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

【问题讨论】:

  • 122 在 else 块中
  • 您的错误信息不清楚。多解释理解逻辑和流程
  • 打开一个新问题,在那里描述你的问题,然后给我一个链接。

标签: java android session


【解决方案1】:

您似乎没有创建sharedpreferences 对象。 你需要类似的东西

 sharedpreferences = new SharedPreferences();

editor = sharedpreferences.edit();之前

【讨论】:

  • 类声明后初始化(全局变量)
  • 你刚刚定义了它,但在 editor = sharedpreferences.edit(); 中引用它之前还没有创建对象;
  • user987339 你能帮帮我吗.. 同一代码中还有一个问题。现在,当我解析我的 Api 数据时,我无法从数据中提取特定字段(如电子邮件 ID)..
  • 无论如何这里是链接stackoverflow.com/questions/36331796/…
【解决方案2】:

如您所见,您会发现错误日志:

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

这意味着您的代码必须存在一些初始化错误。通过查看代码的每一行。我发现你已经声明了

SharedPreferences sharedpreferences; //declared but not Initialized.

你需要在使用之前初始化这个变量sharedpreferences

sharedpreferences = new SharedPreferences(); 
//You may pass argument as per this class Constructors as per your need.

这可能会解决您的问题。我想。

【讨论】:

  • thanx buddy 明白了... :)
猜你喜欢
  • 2018-10-30
  • 2010-11-04
  • 2014-07-09
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多