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;
    }
abhishek
  • 857
  • 3
  • 13
  • 34

2 Answers2

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

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

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

Using Application context everywhere?

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • not understand ? if app is already crashed at 2 then how you reach to 4 ? – Dheeresh Singh Jun 14 '12 at 08: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 '12 at 08:58