将 ResponseListener 传递给请求 class 出现错误

Passing ResponseListener to Request class getting error

我是创建 Android 应用程序的新手。我想要做的是将变量传递给 php 服务并获得真或假。这是我的代码

package com.fishingtournaments.tournamentapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

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

public class LoginActivity extends AppCompatActivity {
 TextView ats;
 EditText qrCode_edit;
 Button check_button;

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

    ats = (TextView) findViewById(R.id.text_textView);
    qrCode_edit = (EditText) findViewById(R.id.qrCode_editText);
    check_button = (Button) findViewById(R.id.check_button);


    check_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String qrCode = qrCode_edit.getText().toString();
            //ats.setText(qrCode);
            Response.Listener<String> responseListener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");

                        if (success){

                            ats.setText("Toks vartotojas yra");

                        }else{
                            ats.setText("Tokio vartotojo nera");

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            };


            LoginRequest loginRequest;
            loginRequest = new LoginRequest(qrCode, responseListener);
            RequestQueue queue;
            queue = Volley.newRequestQueue(LoginActivity.this);
            queue.add(loginRequest);
        }
    });
}

}

这是我的登录请求class

    package com.fishingtournaments.tournamentapp;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.Map;

/**
 * Created by manta on 2017-02-08.
 */

public class LoginRequest extends StringRequest {
    public static String LOGIN_REQUEST_URL = "http://suvalkijossapalas.devpanda.eu/checkQr.php";
    public Map<String, String> params;

    public LoginRequest(String qrcode, Response.Listener<String> listener){
        super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
        params.put("qrCode",qrcode);
    }

    public Map<String,String> getParams(){
        return params;
    }

}

当我尝试在模拟器上运行 运行 应用程序并按下“检查”按钮时,我的应用程序崩溃并出现此错误:

                                                                             java.lang.NullPointerException: Attempt to invoke interface method

'java.lang.Object java.util.Map.put(java.lang.Object, java.lang.Object)' on a null object reference at com.fishingtournaments.tournamentapp.LoginRequest.(LoginRequest.java:19) at com.fishingtournaments.tournamentapp.LoginActivity.onClick(LoginActivity.java:64) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我认为这可能有问题: loginRequest = new LoginRequest(qrCode, responseListener); 我认为它崩溃是因为 responseListener。 任何帮助将不胜感激

问题出在您的 LoginRequest 代码中。特别是这一行:

params.put("qrCode",qrcode);

此处 params 字段等于 null。 您将 params 字段正确声明为 Map<String String>,但您从未创建 Map<String, String> 的新实例来保存数据。

您必须在构造函数中初始化您的字段,如下所示:

public LoginRequest(String qrcode, Response.Listener<String> listener){
    super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
    params = new Map<String, String();
    params.put("qrCode",qrcode);
}

或者在声明之后立即初始化字段,像这样:

public static String LOGIN_REQUEST_URL = "http://suvalkijossapalas.devpanda.eu/checkQr.php";
public Map<String, String> params = new Map<String, String>();