2016-06-09 30 views
0

上使用uncaughtException對話框我的設備有時會錯過相機,所以不幸的是,我的應用程序已停止消息出現,並且app死亡。在活動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;
private camera mCamera; private boolean playing = false;

公共布爾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 
} 
+0

,以顯示你的AlertDialog你不知道我想告訴你,你可以查看logcat的從你正在使用的IDE,並不建議查看logcat到用戶 –

回答

1

您需要通過以防萬一加入

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

謝謝,我添加'AlertDialog alertDialog = builder.create(); alertDialog.show();'AlertDialog.Builder builder = new AlertDialog.Builder(activity)'下的''? – chohyunwook

+0

所以它仍然無法正常工作?現在的結果是什麼? – flobacca

+0

如前面的結果,應用程序沒有死,但沒有顯示對話框 – chohyunwook