在活动android

时间:2016-06-09 08:21:39

标签: java android

我的设备有时会错过相机,所以不幸的是,我的应用程序停止了消息,应用程序就死了。

所以,我尝试使用uncaughtExceptionhandler我想显示logcat对话框。

最近,

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();
}

我运行我的代码。 首先,如果我的设备错过了相机,应用程序不会死。 但没有显示活动和logcat对话框。

我想在我的应用活动上显示logcat对话框

我该如何以编程方式执行此操作?

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)

//全局变量   私人SurfaceHolder mHolder;
  私人相机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 
}

1 个答案:

答案 0 :(得分:1)

您需要添加

来显示AlertDialog
AlertDialog alertDialog = builder.create();alertDialog.show();