Quantcast
Channel: Active questions tagged crash - Stack Overflow
Viewing all articles
Browse latest Browse all 7199

How to detect app crashes and reopen app in android

$
0
0

I found that my app will crash and show empty activities when I want to use it from background that I let it in background after few minutes even one day.

I let super.onCreate(null) in onCreate() when I created all activities of my app. I didn't keep any data by using savedInstanceState.

I tried to use LifecycleObserver to detect when app goes to the background and come back to the foreground, and if app in background over 5 minutes, I will let my app open my first activity to login app again.It didn't enter onResume and crash.

But when I use my app again from background after 10 minutes, it didn't open first activity, but still crash and show empty activities, and these empty activities could't work.

Here is my LifecycleObserver code:

public class HomePageActivity extends AppCompatActivity implements LifecycleObserver {

    ...

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(null);
        setContentView(R.layout.activity_home_page);

        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);

        ....
    }

    ...

    @Override
    public void onStart(){
        super.onStart();
    }

    @Override
    public void onResume(){
        super.onResume();
    }

    @Override
    public void onPause(){
        super.onPause();
    }

    @Override
    public void onStop(){
        super.onStop();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void appInResumeState() {
        if(recordBgDate.length() != 0 && recordBgTime.length() != 0){
            if(recordBgDate.compareTo(MyUtility.getSystemDate()) == 0){
                if(MyUtility.getMinWhenAppInBackground(recordBgTime) < 5){
                    recordBgDate = "";
                    recordBgTime = "";
                    return;
                }else{
                    recordBgDate = "";
                    recordBgTime = "";
                    jumpToSpecificActivityAndFinish(CoverActivity.class);
                }
            }else{
                recordBgDate = "";
                recordBgTime = "";
                jumpToSpecificActivityAndFinish(CoverActivity.class);
            }
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void appInPauseState() {
        recordBgDate = MyUtility.getSystemDate(); // yyyyMMdd.
        recordBgTime = MyUtility.getSystemDateAndTime(); // yyyyMMddHHmmss.
    }

}

Here are I get the system time function and count the time:

public static final String getSystemDateAndTime() {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
    formatter.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    Date curDate = new Date(System.currentTimeMillis());
    String time = formatter.format(curDate);
    return time;
}

public static final String getSystemDate() {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    formatter.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    Date curDate = new Date();
    String time = formatter.format(curDate);
    return time;
}

public static final Integer getMinWhenAppInBackground(String _backgroundTime){
    String nowTime = getSystemDateAndTime();
    return (Integer.valueOf(nowTime.substring(8, 10)) - Integer.valueOf(_backgroundTime.substring(8, 10))) * 60 + (Integer.valueOf(nowTime.substring(10, 12)) - Integer.valueOf(_backgroundTime.substring(10, 12)));
}

I don't really know how to fix it, it has any way to detect my app is crash or even OOM, and I can reopen it, thank you!

Here is my log that my app crashes after I want to use it and it in background after few minutes.

--------- beginning of crash
12-24 16:04:03.968 E/AndroidRuntime(14610): FATAL EXCEPTION: main
12-24 16:04:03.968 E/AndroidRuntime(14610): Process: com.gmpsykr.each, PID: 14610
12-24 16:04:03.968 E/AndroidRuntime(14610): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=null} to activity {com.gmpsykr.each/com.gmpsykr.each.Post.PostActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getAbsolutePath()' on a null object reference
12-24 16:04:03.968 E/AndroidRuntime(14610):     at android.app.ActivityThread.deliverResults(ActivityThread.java:4297)
12-24 16:04:03.968 E/AndroidRuntime(14610):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4347)
12-24 16:04:03.968 E/AndroidRuntime(14610):     at android.app.ActivityThread.-wrap20(ActivityThread.java)
12-24 16:04:03.968 E/AndroidRuntime(14610):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
12-24 16:04:03.968 E/AndroidRuntime(14610):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-24 16:04:03.968 E/AndroidRuntime(14610):     at android.os.Looper.loop(Looper.java:173)
12-24 16:04:03.968 E/AndroidRuntime(14610):     at android.app.ActivityThread.main(ActivityThread.java:6459)
12-24 16:04:03.968 E/AndroidRuntime(14610):     at java.lang.reflect.Method.invoke(Native Method)
12-24 16:04:03.968 E/AndroidRuntime(14610):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938)
12-24 16:04:03.968 E/AndroidRuntime(14610):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
12-24 16:04:03.968 E/AndroidRuntime(14610): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getAbsolutePath()' on a null object reference
12-24 16:04:03.968 E/AndroidRuntime(14610):     at com.gmpsykr.each.Post.PostActivity.onActivityResult(PostActivity.java:416)
12-24 16:04:03.968 E/AndroidRuntime(14610):     at android.app.Activity.dispatchActivityResult(Activity.java:6926)
12-24 16:04:03.968 E/AndroidRuntime(14610):     at android.app.ActivityThread.deliverResults(ActivityThread.java:4293)
12-24 16:04:03.968 E/AndroidRuntime(14610):     ... 9 more
--------- beginning of system
12-26 09:48:11.926 E/BatSS   ( 2413): no controller energy info supplied
12-26 09:48:11.932 E/BatSS   ( 2413): no controller energy info supplied
12-26 09:48:24.217 E/BatSS   ( 2413): no controller energy info supplied
12-26 09:48:24.446 E/ConnectivityService( 2413): Exception in removeRoute: java.lang.IllegalStateException: command '5455 network route remove 111 wlan0 fe80::/64' failed with '400 5455 removeRoute() failed (No such device)'
12-26 09:48:24.448 E/ConnectivityService( 2413): Exception in removeRoute: java.lang.IllegalStateException: command '5456 network route remove 111 wlan0 172.16.16.0/24' failed with '400 5456 removeRoute() failed (No such device)'
12-26 09:48:24.451 E/ConnectivityService( 2413): Exception in removeRoute: java.lang.IllegalStateException: command '5457 network route remove 111 wlan0 0.0.0.0/0 172.16.16.1' failed with '400 5457 removeRoute() failed (No such device)'
12-26 09:48:24.462 E/Nat464Xlat( 2413): startClat: Can't start clat on null interface
12-26 09:48:24.487 E/Nat464Xlat( 2413): startClat: Can't start clat on null interface
12-26 09:48:34.883 E/BatSS   ( 2413): no controller energy info supplied
12-26 09:48:34.893 E/BatSS   ( 2413): no controller energy info supplied
--------- beginning of main
12-26 09:48:36.628 E/CompactDiskManagerImpl.cpp(16818): Failed to remove new folder structure directories: No such file or directory
12-26 09:48:37.042 E/MDH     (10354): [Auth] Failed to get token with scope [oauth2:https://www.googleapis.com/auth/mdh https://www.googleapis.com/auth/webhistory]
12-26 09:48:37.640 E/MDH     (10354): [Auth] Failed to get token with scope [oauth2:https://www.googleapis.com/auth/mdh https://www.googleapis.com/auth/webhistory]
12-26 09:48:37.644 E/MDH     (10354): [Footprints] Sync execution exception
12-26 09:48:37.649 E/MDH     (10354): [Footprints] Sync execution exception
12-26 09:48:39.055 E/art     (16818): No implementation found for int com.facebook.soloader.MergedSoMapping$Invoke_JNI_OnLoad.libmobileconfigonomnistore_jni_so() (tried Java_com_facebook_soloader_MergedSoMapping_00024Invoke_1JNI_1OnLoad_libmobileconfigonomnistore_1jni_1so and Java_com_facebook_soloader_MergedSoMapping_00024Invoke_1JNI_1OnLoad_libmobileconfigonomnistore_1jni_1so__)
12-26 09:48:39.283 E/WifiHAL ( 2413): handleResponse: QCA_WLAN_VENDOR_ATTR_LL_STATS_NUM_RADIOS not found
12-26 09:48:39.283 E/WifiHAL ( 2413): nl80211: requestResponse->nl_recvmsgs failed: -5
12-26 09:48:39.757 E/MDH     (10354): [Auth] Failed to get token with scope [oauth2:https://www.googleapis.com/auth/mdh https://www.googleapis.com/auth/webhistory]
12-26 09:48:40.695 E/MDH     (10354): [Auth] Failed to get token with scope [oauth2:https://www.googleapis.com/auth/mdh https://www.googleapis.com/auth/webhistory]
12-26 09:48:40.700 E/MDH     (10354): [Footprints] Sync execution exception
12-26 09:48:40.704 E/MDH     (10354): [Footprints] Sync execution exception
12-26 09:48:41.517 E/dalvik-internals(16818): skipping IsUpToDate check for incoming dex file
12-26 09:48:41.519 E/dalvik-internals(16818): skipping IsUpToDate check for incoming dex file
12-26 09:48:41.519 E/dalvik-internals(16818): skipping HasCollisions check for incoming dex file
12-26 09:48:41.519 E/dalvik-internals(16818): skipping IsUpToDate check for incoming dex file
12-26 09:48:41.520 E/dalvik-internals(16818): skipping IsUpToDate check for incoming dex file
12-26 09:48:41.520 E/dalvik-internals(16818): skipping HasCollisions check for incoming dex file
12-26 09:48:41.521 E/dalvik-internals(16818): skipping IsUpToDate check for incoming dex file
12-26 09:48:41.523 E/dalvik-internals(16818): skipping IsUpToDate check for incoming dex file
12-26 09:48:41.523 E/dalvik-internals(16818): skipping HasCollisions check for incoming dex file
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818): ActivityRecognition Permission is missing
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818): java.lang.SecurityException: Activity detection usage requires the ACTIVITY_RECOGNITION permission
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at android.os.Parcel.readException(Parcel.java:1692)
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at android.os.Parcel.readException(Parcel.java:1645)
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at com.google.android.gms.internal.location.zza.A01()
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at com.google.android.gms.internal.location.zzap.DQy()
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at X.47F.A0F()
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at X.47F.A0G(:5)
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at X.476.Aj9(:71)
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at X.44a.Aj9(:5)
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at X.44K.A0A(:135)
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at X.5Lx.Ctx()
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at X.5Lw.run(:93)
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at android.os.Handler.handleCallback(Handler.java:751)
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at android.os.Handler.dispatchMessage(Handler.java:95)
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at android.os.Looper.loop(Looper.java:173)
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at android.os.HandlerThread.run(HandlerThread.java:61)
12-26 09:48:42.075 E/fb4a.ActivityRecognition(16818):   at X.1o5.run(:0)
12-26 09:48:42.308 E/WifiHAL ( 2413): handleResponse: QCA_WLAN_VENDOR_ATTR_LL_STATS_NUM_RADIOS not found
12-26 09:48:42.308 E/WifiHAL ( 2413): nl80211: requestResponse->nl_recvmsgs failed: -5
12-26 09:48:42.334 E/ACRA    (16937): Initializing ANR detector for process: com.facebook.appmanager
12-26 09:48:42.398 E/SQLiteLog(16937): (14) cannot open file at line 32456 of [bda77dda96]
12-26 09:48:42.398 E/SQLiteLog(16937): (14) os_unix.c:32456: (2) open(/data/user/0/com.facebook.appmanager/databases/androidx.work.workdb) - 
12-26 09:48:42.398 E/SQLiteConnection(16937): DB info: sqlite3_open_v2, path: /data/user/0/com.facebook.appmanager/databases/androidx.work.workdb, flag: 2, ret: 14
12-26 09:48:42.398 E/SQLiteConnection(16937): DB info: errno = 2, errno message = No such file or directory
12-26 09:48:42.402 E/SQLiteDatabase(16937): Failed to open database '/data/user/0/com.facebook.appmanager/databases/androidx.work.workdb'.
12-26 09:48:42.402 E/SQLiteDatabase(16937): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:239)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:223)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:468)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:190)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:182)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:878)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:858)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:756)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:731)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at com.facebook.oxygen.common.androidx.workmanager.initializer.b.a(WorkManagerDatabase.java:20)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at com.facebook.oxygen.appmanager.app.c.b(AppManagerApplicationImpl.java:19)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at com.facebook.oxygen.common.e.b.d.onCreate(OxpDelegatingApplication.java:68)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1037)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5662)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.app.ActivityThread.-wrap2(ActivityThread.java)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1573)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.os.Looper.loop(Looper.java:173)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at android.app.ActivityThread.main(ActivityThread.java:6459)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at java.lang.reflect.Method.invoke(Native Method)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938)
12-26 09:48:42.402 E/SQLiteDatabase(16937):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
12-26 09:48:42.722 E/ActivityThread(16937): Failed to find provider info for com.facebook.appmanager.nekodirect
12-26 09:48:43.164 E/MDH     (10354): [Auth] Failed to get token with scope [oauth2:https://www.googleapis.com/auth/mdh https://www.googleapis.com/auth/webhistory]
12-26 09:48:43.627 E/MDH     (10354): [Auth] Failed to get token with scope [oauth2:https://www.googleapis.com/auth/mdh https://www.googleapis.com/auth/webhistory]
12-26 09:48:43.638 E/MDH     (10354): [Footprints] Sync execution exception
12-26 09:48:43.644 E/MDH     (10354): [Footprints] Sync execution exception
12-26 09:48:45.347 E/WifiHAL ( 2413): handleResponse: QCA_WLAN_VENDOR_ATTR_LL_STATS_NUM_RADIOS not found
12-26 09:48:45.347 E/WifiHAL ( 2413): nl80211: requestResponse->nl_recvmsgs failed: -5
12-26 09:48:48.395 E/WifiHAL ( 2413): handleResponse: QCA_WLAN_VENDOR_ATTR_LL_STATS_NUM_RADIOS not found
12-26 09:48:48.395 E/WifiHAL ( 2413): nl80211: requestResponse->nl_recvmsgs failed: -5
12-26 09:48:49.264 E/MDH     (10354): [Auth] Failed to get token with scope [oauth2:https://www.googleapis.com/auth/mdh https://www.googleapis.com/auth/webhistory]
12-26 09:48:49.705 E/MDH     (10354): [Auth] Failed to get token with scope [oauth2:https://www.googleapis.com/auth/mdh https://www.googleapis.com/auth/webhistory]
12-26 09:48:49.708 E/MDH     (10354): [Footprints] Sync execution exception
12-26 09:48:49.712 E/MDH     (10354): [Footprints] Sync execution exception
12-26 09:48:50.792 E/WakeLock( 2886): GCM_CONN_ALARM release without a matched acquire!
12-26 09:48:51.331 E/MDH     (10354): [Auth] Failed to get token with scope [oauth2:https://www.googleapis.com/auth/mdh https://www.googleapis.com/auth/webhistory]
12-26 09:48:51.430 E/WifiHAL ( 2413): handleResponse: QCA_WLAN_VENDOR_ATTR_LL_STATS_NUM_RADIOS not found
12-26 09:48:51.430 E/WifiHAL ( 2413): nl80211: requestResponse->nl_recvmsgs failed: -5
12-26 09:48:51.897 E/MDH     (10354): [Auth] Failed to get token with scope [oauth2:https://www.googleapis.com/auth/mdh https://www.googleapis.com/auth/webhistory]
12-26 09:48:51.901 E/MDH     (10354): [Footprints] Sync execution exception
12-26 09:48:51.905 E/MDH     (10354): [Footprints] Sync execution exception
12-26 09:48:53.791 E/WifiHAL ( 2413): handleResponse: QCA_WLAN_VENDOR_ATTR_LL_STATS_NUM_RADIOS not found
12-26 09:48:53.791 E/WifiHAL ( 2413): nl80211: requestResponse->nl_recvmsgs failed: -5
12-26 09:48:53.794 E/BatSS   ( 2413): no controller energy info supplied
12-26 09:48:53.800 E/BatSS   ( 2413): no controller energy info supplied
12-26 09:48:54.162 E/SmartNS_Utility(14446): read /sys/class/dual_role_usb/otg_default/data_role failed: java.io.FileNotFoundException: /sys/class/dual_role_usb/otg_default/data_role (No such file or directory)
...

Base on the log, I have some new question:

In my app I need to create folder to put some images, the log show that there is something wrong when I create folder.

Maybe I didn't close the File when the folder is exist, but I don't know how to close it. Can anyone tell me how to fix that, thank you!

Here is my code:

File storageDir = new File(this.getExternalFilesDir(null).getAbsolutePath(), "myFolder");      
createAppFolder(storageDir);


 public static final void createAppFolder(File _storageDir) {
        if (!_storageDir.exists()) {
            boolean rv = _storageDir.mkdir();
            Log.e("TAG", "state: " + (rv ? "success" : "fail"));
        } else {
            Log.e("TAG", "folder exist");
        }
    }

Viewing all articles
Browse latest Browse all 7199

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>