我想創建一個登錄活動,我驗證並檢查值是否正確,而不是null請幫助我...在哪裏點擊登錄按鈕以下代碼我的應用程序崩潰。請提前幫助我。 我是新來的Android Studio編程請感謝登錄按鈕點擊應用程序崩潰點擊php連接和mysql連接
LoginActivity.java
package com.map.laurence.dbtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class Login extends AppCompatActivity {
EditText user,pass;
Button login,register;
String loginUrl = "http://192.168.56.1/login.php";
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
register = (Button) findViewById(R.id.btnRegister);
login = (Button) findViewById(R.id.btnLogin);
user = (EditText) findViewById(R.id.user);
pass = (EditText) findViewById(R.id.pass);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(Login.this, "You clicked insert", Toast.LENGTH_LONG).show();
StringRequest request = new StringRequest(Request.Method.POST, loginUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("user", user.getText().toString());
return parameters;
}
};
requestQueue.add(request);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
loginUrl, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response.toString());
Toast.makeText(Login.this, "You clicked Login", Toast.LENGTH_LONG).show();
try {
JSONArray students = response.getJSONArray("user");
for (int i = 0; i < students.length(); i++) {
JSONObject student = students.getJSONObject(i);
String user = student.getString("username");
String password = student.getString("password");
String access = student.getString("access");
if (user != null) {
if (password == pass.toString()) {
Toast.makeText(Login.this, "not null", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Login.this, "password incorrect", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(Login.this, "its null", Toast.LENGTH_LONG).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.append(error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);
}
});
}
}
版式文件login.xml我耐心
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.map.laurence.dbtest.Login">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="50sp"
android:id="@+id/user"
android:hint="Username"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50sp"
android:id="@+id/pass"
android:hint="Password"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnLogin"
android:text="LOGIN"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnRegister"
android:text="REGISTER"/>
</LinearLayout>
</RelativeLayout>
的login.php
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
include 'connection.php';
loginStudent();
}
function loginStudent()
{
$user = $_POST["user"];
$pass = $_POST["pass"];
global $connect;
$query = "select * from user where username='$user';";
$result = mysqli_query($connect,$query);
$number_of_rows = mysqli_num_rows($result);
$temp_array = array();
if($number_of_rows > 0){
while($row = mysqli_fetch_assoc($result)){
$temp_array[] = $row;
}
}
header('Content-Type: application/json');
echo json_encode(array("students"=>$temp_array));
mysqli_close($connect);
}
?>
這是在我的logcat我的錯誤
12月8日至29日:16:39.731 3460-3460/com.map.laurence.dbtest E/AndroidRuntime:致命異常:主 過程:COM .map.laurence.dbtest,PID:3460 java.lang.NullPointerException at com.map.laurence.dbtest.Login $ 1.onClick(Login.java:72) at android.view.View.performClick(View.java :4438) at android.view.View $ PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android .app.ActivityThread.main(ActivityThread.java:5019) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com。 android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method )
把你的Logcat。 –
剛剛更新了錯誤sir – LAW
我想你忘了初始化你的requestQueue參數。 –