我有以下代码(我知道其中一些可能是多余的,如果有,请告诉我)。我无法做的是停止调用一次又一次地更新位置。我尝试了一些不同的方法来阻止它(它们存在于代码中,但已注释)。有时我会
java.lang.RuntimeException: Unable to start activity ComponentInfo{jangcy.emergency/jangcy.emergency.ReportStartActivity}: java.lang.IllegalArgumentException: invalid listener: null
有时候
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.location.LocationManager.removeUpdates(android.location.LocationListener)' on a null object reference
如果我试图用一些int来阻止它(就像现在这样),它并没有真正停止。如果我删除if,它会在将位置发送到DB之前停止。
请帮忙吗?
package jangcy.emergency;
import android.*;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class ReportStartActivity extends AppCompatActivity {
private String user;
TextView textView;
private double latitude = 500;
private double longitude = 500;
double old_longitude = 499;
double old_latitude = 499;
LocationManager locationManager;
LocationListener locationListener;
SharedPreferences sharedPref;
private int idZgloszenia;
public int counter = 0;
private static final String url_start_report = "http://192.168.0.12:80/emergency/start_report.php";
private StringRequest request;
private RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report_start);
requestQueue = Volley.newRequestQueue(this);
textView = (TextView) findViewById(R.id.testTextView);
textView.setText("jangcy");
sharedPref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
user = sharedPref.getString("loginKey", null);
int ik = geoLocation();
if (ik == 5){
locationManager.removeUpdates(locationListener);
locationManager = null;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION,
android.Manifest.permission.INTERNET
}, 10);
return;
}
} else {
//configureButton();
}
}
public int geoLocation()
{
final int[] do_zwrotu = new int[1];
textView.setText(String.valueOf(counter));
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
textView.setText(String.valueOf(longitude) + " " + String.valueOf(latitude));
if (longitude >= -180 && longitude <= 180 && latitude >= -90 && latitude <= 90) {
do_zwrotu[0] = 5;
textView.setText(String.valueOf(longitude) + " " + String.valueOf(latitude) + user);
startReport(user, longitude, latitude);
/*locationManager.removeUpdates(locationListener);
locationManager = null;*/
/*locationManager.removeUpdates(this);
locationManager = null;*/
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Intent in = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(in);
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
return do_zwrotu[0];
}
private void startReport(final String user, final double longitude, final double latitude) {
request = new StringRequest(Request.Method.POST, url_start_report, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
System.out.println(response);
JSONObject jsonObject = new JSONObject(response);
int success = jsonObject.getInt("success");
if (success == 1) {
/*
locationManager.removeUpdates(locationListener);
locationManager = null;*/
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
//idZgloszenia = jsonObject.getInt("id_zgloszenia");
//textView.setText(idZgloszenia);
} else {
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("gps_x", Double.toString(longitude));
hashMap.put("gps_y", Double.toString(latitude));
hashMap.put("uzytkownik", user);
return hashMap;
}
};
requestQueue.add(request);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 10:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
//configureButton();
return;
}
}
private void configureButton() {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
textView.setText("jangcy2");
}
}