Java Android截取-在对字符串调用.length()时引发空指针异常,但我不';不要在任何地方使用.length()
所以我尝试将一些数据上传到MySQL数据库。我从一个类(LoginActivity)中提取一个变量,并将其放入另一个类(PartySetup),以便将这个变量和一些新变量一起传递到MySQL数据库中。在尝试提取这个新变量之前,一切都进行得很好——我所做的唯一更改是在LoginActivity中创建一个公共变量,将“user”添加到CreatePartyRequest,并修复我的PHP 有人知道为什么会这样吗?我收到错误消息Java Android截取-在对字符串调用.length()时引发空指针异常,但我不';不要在任何地方使用.length(),java,php,android,mysql,Java,Php,Android,Mysql,所以我尝试将一些数据上传到MySQL数据库。我从一个类(LoginActivity)中提取一个变量,并将其放入另一个类(PartySetup),以便将这个变量和一些新变量一起传递到MySQL数据库中。在尝试提取这个新变量之前,一切都进行得很好——我所做的唯一更改是在LoginActivity中创建一个公共变量,将“user”添加到CreatePartyRequest,并修复我的PHP 有人知道为什么会这样吗?我收到错误消息试图在空对象引用上调用虚拟方法“int java.lang.String.
试图在空对象引用上调用虚拟方法“int java.lang.String.length()”
——我不知道这里什么是空的
PartySetup.java:
public class PartySetup extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
private Location location;
private TextView tempLatitude;
private TextView tempLongitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_party_setup);
final EditText tempPartyName = (EditText) findViewById(R.id.party_name);
final EditText tempHostName = (EditText) findViewById(R.id.host_name);
final Button startParty = (Button) findViewById(R.id.create_party);
final CheckBox locationButton = (CheckBox) findViewById(R.id.set_location);
final String email = LoginActivity.publicEmail;
tempLatitude = (TextView) findViewById(R.id.latitude_text);
tempLongitude = (TextView) findViewById(R.id.longitude_text);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
mGoogleApiClient.connect();
startParty.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (locationButton.isChecked() && tempPartyName != null){
final String partyName = tempPartyName.getText().toString();
final String hostName;
if (tempHostName == null){
hostName = "";
}
else hostName = tempHostName.getText().toString();
final String latitude = tempLatitude.getText().toString();
final String longitude = tempLongitude.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>(){
@Override
public void onResponse(String response){
try{
Log.i("TAG", response);
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if(success){
Intent intent = new Intent(PartySetup.this, HomePage.class);
startActivity(intent);
}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(PartySetup.this);
builder.setMessage("Invalid Party Nickname")
.setNegativeButton("Try Again", null)
.create()
.show();
}
} catch (JSONException e){
e.printStackTrace();
}
}
};
CreatePartyRequest createPartyRequest = new CreatePartyRequest(partyName, hostName, latitude, longitude, email, responseListener);
RequestQueue queue = Volley.newRequestQueue(PartySetup.this);
queue.add(createPartyRequest);
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(PartySetup.this);
builder.setMessage("Please check the location box")
.setNegativeButton("Try Again", null)
.create()
.show();
}
}
});
}
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
try{
location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
catch (SecurityException e){
e.printStackTrace();
}
if (location != null){
tempLatitude.setText(String.valueOf(location.getLatitude()));
tempLongitude.setText(String.valueOf(location.getLongitude()));
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
公共类PartySetup扩展AppCompative实现GoogleAppClient.ConnectionCallbacks、GoogleAppClient.OnConnectionFailedListener{
私人GoogleapClient MGoogleapClient;
私人位置;
私有文本视图模板;
私有文本视图模板;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u party\u设置);
最终EditText tempPartyName=(EditText)findViewById(R.id.party_名称);
final EditText tempHostName=(EditText)findViewById(R.id.host\u name);
最终按钮startParty=(按钮)findViewById(R.id.create\u party);
最终复选框位置按钮=(复选框)findViewById(R.id.set_位置);
最终字符串email=LoginActivity.publiceemail;
tempLatitude=(TextView)findViewById(R.id.latitude\u text);
Templongitute=(TextView)findViewById(R.id.longitude\u text);
if(mGoogleApiClient==null){
mgoogleapclient=新的Googleapclient.Builder(此)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.addApi(LocationServices.API)
.build();
}
mGoogleApiClient.connect();
startParty.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(locationButton.isChecked()&&tempPartyName!=null){
最后一个字符串partyName=tempPartyName.getText().toString();
最终字符串主机名;
if(tempHostName==null){
主机名=”;
}
else hostName=tempHostName.getText().toString();
最终字符串纬度=tempLatitude.getText().toString();
最终字符串经度=Templongitute.getText().toString();
Response.Listener responseListener=新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
Log.i(“标签”,响应);
JSONObject jsonResponse=新的JSONObject(响应);
boolean success=jsonResponse.getBoolean(“success”);
如果(成功){
意向意向=新意向(PartySetup.this,HomePage.class);
星触觉(意向);
}
否则{
AlertDialog.Builder=新建AlertDialog.Builder(PartySetup.this);
builder.setMessage(“无效的参与方昵称”)
.setNegativeButton(“重试”,null)
.create()
.show();
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
};
CreatePartyRequest CreatePartyRequest=新建CreatePartyRequest(partyName、主机名、纬度、经度、电子邮件、responseListener);
RequestQueue=Volley.newRequestQueue(PartySetup.this);
添加(createPartyRequest);
}
否则{
AlertDialog.Builder=新建AlertDialog.Builder(PartySetup.this);
builder.setMessage(“请选中位置框”)
.setNegativeButton(“重试”,null)
.create()
.show();
}
}
});
}
受保护的void onStart(){
mGoogleApiClient.connect();
super.onStart();
}
受保护的void onStop(){
mGoogleApiClient.disconnect();
super.onStop();
}
@凌驾
未连接的公共无效(@Nullable Bundle){
试一试{
location=LocationServices.FusedLocationApi.getLastLocation(mgoogleapClient);
}
捕获(安全异常e){
e、 printStackTrace();
}
如果(位置!=null){
tempLatitude.setText(String.valueOf(location.getLatitude());
setText(String.valueOf(location.getLongitude());
}
}
@凌驾
公共空间连接暂停(int i){
}
@凌驾
public void onconnection失败(@NonNull ConnectionResult ConnectionResult){
}
}
CreatePartyRequest.java:
public class CreatePartyRequest extends StringRequest {
private static final String CREATE_PARTY_REQUEST_URL = "http://10.0.2.2:8080/android_login_api/create_party_request.php";
private Map<String, String> params;
public CreatePartyRequest(String partyName, String hostName, String latitude, String longitude, String email, Response.Listener<String> listener){
super(Method.POST, CREATE_PARTY_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("partyName", partyName);
params.put("hostName", hostName);
params.put("latitude", latitude);
params.put("longitude", longitude);
params.put("user", email);
}
@Override
public Map<String, String> getParams(){
return params;
}
}
公共类CreatePartyRequest扩展了StringRequest{
私有静态最终字符串CREATE\u PARTY\u REQUEST\u URL=”http://10.0.2.2:8080/android_login_api/create_party_request.php";
私有映射参数;
公共CreatePartyRequest(字符串partyName、字符串主机名、字符串纬度、字符串经度、字符串电子邮件、响应.侦听器){
super(Method.POST,CREATE\u PARTY\u REQUEST\u URL,listener,null);
params=新的HashMap();
参数put(“partyName”,partyName);
参数put(“主机名”,主机名);
public class LoginActivity extends AppCompatActivity{
public static String publicEmail;
@Override
protected void onCreate(Bundle savedInstancedState){
super.onCreate(savedInstancedState);
setContentView(R.layout.activity_login);
final EditText tempEmail = (EditText) findViewById(R.id.email);
final EditText tempPassword = (EditText) findViewById(R.id.password);
final Button loginButton = (Button) findViewById(R.id.btnLogin);
final Button toRegisterScreen = (Button) findViewById(R.id.btnLinkToRegisterScreen);
if (SaveSharedPreference.getUserName(LoginActivity.this).length() != 0){
// If the user's already been logged in, skip the login screen
Intent intent = new Intent(LoginActivity.this, OneTimeWelcome.class);
startActivity(intent);
finish();
}
toRegisterScreen.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
LoginActivity.this.startActivity(registerIntent);
}
});
loginButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
final String email = tempEmail.getText().toString();
publicEmail = email;
final String password = tempPassword.getText().toString();
//Response received from the server
Response.Listener<String> responseListener = new Response.Listener<String>(){
@Override
public void onResponse(String response){
try{
Log.i("TAG", response);
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if(success){
String name = jsonResponse.getString("name");
Intent intent = new Intent(LoginActivity.this, OneTimeWelcome.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("name", name);
intent.putExtra("email", email);
LoginActivity.this.startActivity(intent);
finish();
}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage("Login Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
}
catch(JSONException e){
e.printStackTrace();
}
SaveSharedPreference.setUserName(LoginActivity.this, email);
}
};
LoginRequest loginRequest = new LoginRequest(email, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(loginRequest);
}
});
}
}
<?php
$con = mysqli_connect("127.0.0.1" , "[username]" , "[password]" , "android_api");
$partyName = $_POST["partyName"];
$hostName = $_POST["hostName"];
$latitude = $_POST["latitude"];
$longitude = $_POST["longitude"];
$email = $_POST["email"];
global $con, $partyName, $hostName, $latitude, $longitude;
$statement = mysqli_prepare($con, "INSERT INTO parties (party_name, host_name, latitude, longitude, email) VALUES (?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "sssss", $partyName, $hostName, $latitude, $longitude, $email);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
$emailStatement = mysqli_prepare($con, "SELECT * FROM" )
$response = array();
$response["success"] = true;
echo json_encode($response);
?>