Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.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 会话实现不工作_Java_Android_Session - Fatal编程技术网

Java 会话实现不工作

Java 会话实现不工作,java,android,session,Java,Android,Session,我正在设计一个登录表单,在其中实现会话。在实现会话之后,我得到以下错误。无法理解它为什么会发生 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.Handle

我正在设计一个登录表单,在其中实现会话。在实现会话之后,我得到以下错误。无法理解它为什么会发生

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\u 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>

似乎您没有创建
SharedReferences
对象。 你需要像这样的东西

 sharedpreferences = new SharedPreferences();

editor=SharedReferences.edit()之前

当您看到错误日志时,您会发现:

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
这意味着您的代码中一定有一些初始化错误。通过检查代码的每一行。我发现你已经申报了

SharedReferences SharedReferences//已声明但未初始化。

在使用该变量之前,需要初始化该变量
SharedReferences

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

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

122在else块中,您的错误消息不清楚。解释更多内容以了解逻辑和流程打开一个新问题,在那里描述您的问题并向我发送指向它的链接。它在类声明(全局变量)后初始化,您刚刚定义了它,但在editor=sharedpreferences.edit()中引用它之前没有创建对象;你能帮我吗。。同一代码中还出现了一个问题。现在,当我解析我的Api数据时,我无法从数据中提取特定字段(如电子邮件id)…无论如何,这是thanx buddy获得的链接…:)
sharedpreferences = new SharedPreferences(); 
//You may pass argument as per this class Constructors as per your need.