简体   繁体   中英

use uncaughtException dialog on activity android

my device sometimes misses the camera, so unfortunately , my app has stopped message appears and app dies.

so, I try use uncaughtExceptionhandler I want to show the logcat dialog .

recently ,

MainActivity.java

@Override
protected void onCreate(final Bundle savedInstanceStae) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread.setDefaultUncaughtExceptionHandler(new UnhandledExceptionHandler(this));

UnhandledExceptionHandler.java

public class UnhandledExceptionHandler implements Thread.UncaughtExceptionHandler {
private static final String TAG = "UnUnHandler";
private final Activity activity;

public UnhandledExceptionHandler(final Activity activity) {
    this.activity = activity;
}

public void uncaughtException(Thread unusedThread, final Throwable e) {
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            String title = "Fatal error: " + getTopLevelCauseMessage(e);
            String msg = getRecursiveStackTrace(e);
            TextView errorView = new TextView(activity);
            errorView.setText(msg);
            errorView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 8);
            ScrollView scrollingContainer = new ScrollView(activity);
            scrollingContainer.addView(errorView);
            Log.e(TAG, title + "\n\n" + msg);
            DialogInterface.OnClickListener listener =
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            System.exit(1);
                        }
                    };
            AlertDialog.Builder builder =
                    new AlertDialog.Builder(activity);
            builder
                    .setTitle(title)
                    .setView(scrollingContainer)
                    .setPositiveButton("Exit", listener).show();
        }
    });
}

private static String getTopLevelCauseMessage(Throwable t) {
    Throwable topLevelCause = t;
    while (topLevelCause.getCause() != null) {
        topLevelCause = topLevelCause.getCause();
    }
    return topLevelCause.getMessage();

}

private static String getRecursiveStackTrace(Throwable t) {
    StringWriter writer = new StringWriter();
    t.printStackTrace(new PrintWriter(writer));
    return writer.toString();
}

and I run my code. first, if my device misses the camera , the app doesn't die. but not showing activity and logcat dialog.

I want to show logcat dialog on my app activity

How can I do this programatically ?

logcat

tTTTTTTFatal error: startPreview failed
java.lang.RuntimeException: startPreview failed
at android.hardware.Camera.startPreview(Native Method)
at kr.co.iosystem.blackeyeonandroid.video.CameraLiveView.cameraPlayStart(CameraLiveView.java:202)
at kr.co.iosystem.blackeyeonandroid.video.CameraLiveView.surfaceCreated(CameraLiveView.java:116)
at android.view.SurfaceView.updateWindow(SurfaceView.java:572)
at android.view.SurfaceView.access$000(SurfaceView.java:86)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:175)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1867)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)

//Global variable private SurfaceHolder mHolder;
private Camera mCamera; private boolean playing = false;

public boolean cameraPlayStart() {

    if (mHolder == null) {

        return false;

    }
    try {

        mCamera.setPreviewDisplay(mHolder);
        mCamera.startFaceDetection();
        mCamera.startPreview();    //occur error

        playing = true;
    } catch (IOException e) {
        return false;
    }

    return true;

}


    @Override
public void surfaceCreated(SurfaceHolder holder) {
cameraPlayStart();//occur error 
}

您需要通过添加来显示AlertDialog

AlertDialog alertDialog = builder.create();alertDialog.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM