在我的android應用程序中,我在啓動屏幕中獲取GPS位置並存儲在會話中。在我的基於位置的片段中,我從數據庫中提取數據,如果用戶想要在點擊操作欄上更改位置,則它將跳轉到SelectLocationActivity
有按鈕檢測我的位置。無法銷燬空對象上的活動LocationTrack.stopListener()'參考
如果用戶點擊按鈕,它的獲取位置和會話存儲,但如果使用單擊後退按鈕與出上做活動的任何行動,我的應用程序得到錯誤無法破壞活動墜毀。
我的代碼:
public class SelectLocationActivity extends AppCompatActivity {
private Button detcbtn;
private Session session;
private ArrayList<String> permissionsToRequest;
private ArrayList<String> permissionsRejected = new ArrayList<>();
private ArrayList<String> permissions = new ArrayList<>();
private final static int ALL_PERMISSIONS_RESULT = 101;
LocationTrack locationTrack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectlocation);
Toolbar toolbar = (Toolbar) findViewById(R.id.stoolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
session = new Session(SelectLocationActivity.this);
detcbtn = (Button) findViewById(R.id.find_location);
detcbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
permissionsToRequest = findUnAskedPermissions(permissions);
//get the permissions we have asked for before but are not granted..
//we will store this in a global list to access later.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (permissionsToRequest.size() > 0) {
requestPermissions(permissionsToRequest.toArray(new String[permissionsToRequest.size()]), ALL_PERMISSIONS_RESULT);
} else {
SetUserLocation();
}
}else{SetUserLocation();}
}
});
}
public void SetUserLocation() {
locationTrack = new LocationTrack(SelectLocationActivity.this);
if (locationTrack.canGetLocation()) {
double longitude = locationTrack.getLongitude();
double latitude = locationTrack.getLatitude();
Geocoder gcd = new Geocoder(SelectLocationActivity.this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = gcd.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses != null && addresses.size() > 0) {
String locality = addresses.get(0).getLocality();
String subLocality = addresses.get(0).getSubLocality();
String address = addresses.get(0).getAddressLine(0);
String state = addresses.get(0).getAdminArea();
String countryn = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();
session.setLocation(latitude, longitude, locality, subLocality);
Toast.makeText(SelectLocationActivity.this,locality +" "+ subLocality, Toast.LENGTH_SHORT).show();
}
} else {
locationTrack.showSettingsAlert();
}
//go to main activity
SetLocation();
}
public void SetLocation(){
Intent intent = new Intent(SelectLocationActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
///GPS Tracker
private ArrayList<String> findUnAskedPermissions(ArrayList<String> wanted) {
ArrayList<String> result = new ArrayList<String>();
for (String perm : wanted) {
if (!hasPermission(perm)) {
result.add(perm);
}
}
return result;
}
private boolean hasPermission(String permission) {
if (canMakeSmores()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED);
}
}
return true;
}
private boolean canMakeSmores() {
return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case ALL_PERMISSIONS_RESULT:
for (String perms : permissionsToRequest) {
if (!hasPermission(perms)) {
permissionsRejected.add(perms);
}
}
if (permissionsRejected.size() > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(permissionsRejected.get(0))) {
showMessageOKCancel("These permissions are mandatory for the application. Please allow access.",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(permissionsRejected.toArray(new String[permissionsRejected.size()]), ALL_PERMISSIONS_RESULT);
}
}
});
return;
}else{SetUserLocation();}
}
}else{SetUserLocation();}
break;
}
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(SelectLocationActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
@Override
protected void onDestroy() {
super.onDestroy();
locationTrack.stopListener();
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
}
上後退按鈕點擊它去我的片段。我需要在我的片段中創建任何OnResume
方法嗎?
請參閱錯誤報告
06-16 12:21:28.061 22173-22173/zesteve.com.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: zesteve.com.myapplication, PID: 22173
java.lang.RuntimeException: Unable to destroy activity {zesteve.com.myapplication/zesteve.com.myapplication.SelectLocationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void zesteve.com.myapplication.location.LocationTrack.stopListener()' on a null object reference
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3861)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3879)
at android.app.ActivityThread.access$1500(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1414)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5468)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void zesteve.com.myapplication.location.LocationTrack.stopListener()' on a null object reference
at zesteve.com.myapplication.SelectLocationActivity.onDestroy(SelectLocationActivity.java:189)
at android.app.Activity.performDestroy(Activity.java:6793)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1143)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3848)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3879)
at android.app.ActivityThread.access$1500(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1414)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5468)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
完成任務。謝謝你的完美 –