我正在设计一个登录表单,我在其中实现会话。实现会话后,我收到以下错误。无法理解为什么会发生这种情况。
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
答案 0 :(得分:2)
您似乎没有创建sharedpreferences
对象。
你需要像
sharedpreferences = new SharedPreferences();
之前editor = sharedpreferences.edit();
答案 1 :(得分:1)
正如您可能会看到错误日志,您会发现:
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.
这可能会解决您的问题。我想。