2017-02-08 15 views
0

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

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); 
     } 
    }); 
} 

} 

这是我LoginRequest类

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$1.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);我认为它崩溃b由于responseListener。 任何帮助将不胜感激

回答

0

问题出在你的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>();