2

I am showing an alert box in my app to enable GPS, app works fine for the first time but crashes on second time, then works fine for third and crashes for fourth time and so on. When crashes it shows error "Is your activity running?". I tried it with handler and separate UI thread, but no success. I am trying this.

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ExceptionHandler.register(this, WebServiceIface.BASE_URL
    + "/exception/log");
  instanceState = savedInstanceState;
  appStatus = AppStatus.getInstance(this);
  appStatus.mFromQrCode = false;
  mhandler = new Handler();
  appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
  isFromLogin = getIntent().getBooleanExtra("LOGIN_FLAG", false);
  isOnPause = false;
  listenForLocation();
  loginIfNeeded();
 }

 private void listenForLocation() {
  gps = GpsLocator.getGpsLocator(this);
  gps.listen();
 }

 @Override
 protected void onResume() {
  super.onResume();  
  gps.destroyGpsDialog();
  listenForLocation();
  compareTimes();
  isResuming = true;
  isOnPause = false;
  isFromRefresh=true;
 }

I am calling alert box from GpsLocator Activity

public void createGpsDialog(){

      final AlertDialog.Builder builder = new AlertDialog.Builder(context);
         builder.setMessage("Yout GPS seems to be disabled, do you want to enable it?").setCancelable(true)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, final int id) {
                 dialog.dismiss();
                    context.startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS),6186);


                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, final int id) {
                     dialog.dismiss();
                }
            });
         alert = builder.create();
         alert.show();
    }

It is very weird that it is crashing only on second time

EDIT..

 private GpsLocator(CheckinNativeActivity context) {
        this.context = context;
        this.observers = new ArrayList<GpsFragment>();
        this.removalList = new ArrayList<GpsFragment>();
    }

    public static GpsLocator getGpsLocator(CheckinNativeActivity cxt) {
        if (ref == null)
            ref = new GpsLocator(cxt);
        return ref;
    }
3
  • What do you have on the "context" variable? Aren't you reseting it? Jun 14, 2012 at 7:40
  • It is main activity context,I am using Fragments here.
    – abhishek
    Jun 14, 2012 at 7:43
  • Please provide the destroyGpsDialog() here Jun 14, 2012 at 8:14

2 Answers 2

1

In Any case check if context is passed, in your case check if context is passed to GPsLocator Activity or not while calling intent for activity

2
  • I am already passing context. please check above, i have added a snippet.
    – abhishek
    Jun 15, 2012 at 12:42
  • try this ,private GpsLocator(CheckinNativeActivity context) { this.observers = new ArrayList<GpsFragment>(); this.removalList = new ArrayList<GpsFragment>(); } public static GpsLocator getGpsLocator(CheckinNativeActivity cxt) { if (ref == null) ref = new GpsLocator(cxt); context=cxt; return ref; }
    – nleshjinde
    Jun 15, 2012 at 14:53
0

try with context.getApplicationContext() here in place of context........

Using Application context everywhere?

2
  • not understand ? if app is already crashed at 2 then how you reach to 4 ? Jun 14, 2012 at 8:13
  • when I start for first time it works fine, then I close my app and restart it, app crashes, then I again start my app it works fine, then crashes when I restart fourth time.
    – abhishek
    Jun 14, 2012 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.