- The value of attribute is null
- The activity hasn't defined in AndroidManifest.xml
- etc
If your application is force close, it will appears a popup unfortunately your app has stopped working.
But this tutorial will handle it. By this tutorial, if your application face an error force close, it won't show a popup nfortunately your app has stopped working. , but the application will open an activity that you chosen.
Maybe the main process like the following :
If Activity B is force close, then go to the MainActivity
PREPARATION
Create 2 java classes :
- ForceCloseException.java
- ForceCloseDebugge
Now modify ForceCloseException.java, so look likes the following code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.putuguna.forceclosehandling; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Build; | |
import java.io.PrintWriter; | |
import java.io.StringWriter; | |
/** | |
* Created by putuguna on 26/10/16. | |
*/ | |
public class ForceCloseException implements java.lang.Thread.UncaughtExceptionHandler { | |
private final Activity myContext; | |
public ForceCloseException(Activity context) { | |
myContext = context; | |
} | |
public void uncaughtException(Thread thread, Throwable exception) { | |
StringWriter stackTrace = new StringWriter(); | |
exception.printStackTrace(new PrintWriter(stackTrace)); | |
StringBuilder errorReport = new StringBuilder(); | |
System.out.println("*****CAUSE OF ERROR*****\n\n"); | |
System.out.println("FORCE CLOSE CAUSED : " + stackTrace.toString()); | |
System.out.println("**** DEVICE INFORMATION ***\n\n" + | |
"Brand : " + Build.BRAND + "\n" + | |
"Device : " + Build.DEVICE + "\n" + | |
"Model : " + Build.MODEL + "\n" + | |
"ID : " + Build.ID + "\n" + | |
"Product : " + Build.PRODUCT + "\n" + | |
"SDK : " + Build.VERSION.SDK + "\n" + | |
"Release : " + Build.VERSION.RELEASE + "\n" + | |
"Incremental : " + Build.VERSION.INCREMENTAL + "/n"); | |
Intent intent = new Intent(myContext, myContext.getClass()); | |
intent.putExtra("bugs", stackTrace.toString()); | |
myContext.startActivity(intent); | |
myContext.finish(); | |
android.os.Process.killProcess(android.os.Process.myPid()); | |
System.exit(10); | |
} | |
} |
Intent intent = new Intent(myContext, myContext.getClass());
you can change myContext.getClass() with activity that you wanna open if force close happens. myContext.getClass() is current activity.
Modify ForceCloseDebugger.java, so look likes the following code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.putuguna.forceclosehandling; | |
import android.app.Activity; | |
/** | |
* Created by putuguna on 26/10/16. | |
*/ | |
public class ForceCloseDebugger { | |
public static void handle(Activity context){ | |
if (!BuildConfig.BUILD_TYPE.equalsIgnoreCase("debug")) { | |
Thread.setDefaultUncaughtExceptionHandler(new ForceCloseException(context)); | |
String errorCaused = context.getIntent().getStringExtra("bugs"); | |
System.out.println("FORCE CLOSE CAUSED BY : " + errorCaused); | |
}else{ | |
Thread.setDefaultUncaughtExceptionHandler(new ForceCloseException(context)); | |
String errorCaused = context.getIntent().getStringExtra("bugs"); | |
System.out.println("FORCE CLOSE CAUSED BY : " + errorCaused); | |
} | |
} | |
} |
ForceCloseDebugger.java help you to see the causes of the error in your Android Studio's logcat.
USAGE
Put ForceCloseDebugger.handle(this); in onCreate() in your activity
For example how the usage :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.putuguna.forceclosehandling; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.Toast; | |
/** | |
* Created by putuguna on 26/10/16. | |
*/ | |
public class SecondActivity extends AppCompatActivity { | |
private Button btnForceCLose; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_force_close); | |
ForceCloseDebugger.handle(this); | |
} | |
} |
That's all.
EmoticonEmoticon