我是創建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。 任何幫助將不勝感激