Java 将android连接到我的EclipseTomcat服务器
我正在eclipse中使用tomcat服务器运行spring启动应用程序,以接受请求并将它们保存在本地数据库中(通过wamp)。使用android作为客户端,我想将登录请求作为从用户输入中获取的json对象发送,但当我按下登录按钮时,我的模拟器应用程序崩溃。 (我还有两个类:UserDAO和User(分数为整数的模型类)) 这是我的android登录活动:Java 将android连接到我的EclipseTomcat服务器,java,android,eclipse,tomcat,spring-boot,Java,Android,Eclipse,Tomcat,Spring Boot,我正在eclipse中使用tomcat服务器运行spring启动应用程序,以接受请求并将它们保存在本地数据库中(通过wamp)。使用android作为客户端,我想将登录请求作为从用户输入中获取的json对象发送,但当我按下登录按钮时,我的模拟器应用程序崩溃。 (我还有两个类:UserDAO和User(分数为整数的模型类)) 这是我的android登录活动: package com.example.mujtaba.quizzer; import android.app.ProgressDialo
package com.example.mujtaba.quizzer;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.StringRequest;
import com.example.mujtaba.quizzer.Activity.QuizMaking;
import com.example.mujtaba.quizzer.Activity.QuizTaking;
import org.w3c.dom.Text;
import java.util.HashMap;
import java.util.Map;
public class Login extends AppCompatActivity {
private Button button;
private TextView username;
private TextView password;
private Spinner role;
private String url = "http://localhost:8080/users/signup";
private RequestQueue queue;
private ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username=(TextView) findViewById(R.id.username);
password=(TextView) findViewById(R.id.password);
button=(Button) findViewById(R.id.button);
role = (Spinner) findViewById(R.id.role);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.role_spinner, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
role.setAdapter(adapter);
}
public void Quiz(View v) { //select a new activity on the basis of role
StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//This code is executed if the server responds, whether or not the response contains data.
//The String 'response' contains the server's response.
}
}, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
@Override
public void onErrorResponse(VolleyError error) {
//This code is executed if there is an error.
}
}) {
protected Map<String, String> getParams() {
Map<String, String> MyData = new HashMap<String,String>();
MyData.put("Username", username.getText().toString() ); //Add the data you'd like to send to the server.
MyData.put("Password",password.getText().toString());
MyData.put("Role",role.getSelectedItem().toString());
MyData.put("Score","0");
return MyData;
}
};
queue.add(MyStringRequest);
}
}
这是一个未初始化的变量。具有空值的指针。因此
queue.add(MyStringRequest);
相等于
null.add(MyStringRequest);
这会导致“NullPointerException”
因此,请在使用前初始化变量。日志中出现了什么错误?哪条语句导致了崩溃?请找出答案。更新问题。有一个链接指向logcat图像@greenappsWell你是#123456,有一个NullPointerException。所以请谷歌。虽然logcat会告诉你,但你没有告诉我们是哪种说法导致的。我没有看到logcat,因为你应该在这里发布logcat的文本。请不要图像。错误:由以下原因引起:java.lang.NullPointerException:尝试调用空对象引用@greenapps上的虚拟方法“com.android.volley.Request com.android.volley.RequestQueue.add(com.android.volley.Request)”
queue.add(MyStringRequest);
null.add(MyStringRequest);