我有一个BroadcastReciever,它调用sms活动获取Gps协调并将其发送到另一个设备。问题是,无论何时我调用getGPSLocation,应用程序都会失败。如果我发表评论它就可以了。有一些位置导致应用程序失败,但我找不到它。请有人帮助找到问题。谢谢你的关注。
public class SMS extends AppCompatActivity {
private String myGPSLocation;
private LocationManager myLocationManager;
boolean issent = false;
String text = "your cordinates ";
SmsManager smsManager = SmsManager.getDefault();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//call message method pass getGPS function which returns string
myLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
//makeUseOfNewLocation(location);
myGPSLocation = location.toString();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
myLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
message(text + getGPSLocation());
}
private String getGPSLocation() {
String cords;
if (myGPSLocation == null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return "PERMITED";
}
Location locationGPS = myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
cords = locationGPS.toString();
return cords;
}
else {
return myGPSLocation;
}
}
public void message(String info)
{
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, SMS.class), 0);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String num = settings.getString("phone_number", "");
if(num != "") {
smsManager.sendTextMessage(num, null, info, pi, null);
issent = true;
this.finish();
} else { Toast.makeText(this, "Phone is not SET ", Toast.LENGTH_SHORT).show(); this.finish();}
}
boolean isSent()
{
return issent;
}
}
logcat的
FATAL EXCEPTION: main
java.lang.IllegalArgumentException: Rect should intersect with child's bounds.
at android.support.design.widget.CoordinatorLayout.offsetChildByInset(CoordinatorLayout.java:1319)
at android.support.design.widget.CoordinatorLayout.onChildViewsChanged(CoordinatorLayout.java:1257)
at android.support.design.widget.CoordinatorLayout$OnPreDrawListener.onPreDraw(CoordinatorLayout.java:1805)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:864)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2146)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1253)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6402)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:561)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
试试这个,
交换这两行。在初始化导致NLP的 myLocationManager 之前,您正在调用getGPSLocation()方法。
message(text + getGPSLocation());
myLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
到
myLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
message(text + getGPSLocation());
你可以再做一次检查 在
private String getGPSLocation() {
String cords;
if (myGPSLocation == null) {
if(myLocationManager != null){
....//your code
}
}