2013-03-27 182 views
0

我有一個Android應用程序,最近神祕地強制關閉一些使用4.x設備的用戶。看看異常堆棧,好像在我的任何代碼運行之前發生錯誤,但我認爲我正在做一些導致此問題的事情。從模糊堆棧跟蹤中查找異常原因

* 如何找到我的原因或堆棧有問題?否則,如果這是一個完整的堆棧跟蹤是否真的有可能在我的任何代碼運行之前Android失敗? *

我覺得後者不可能的,因爲我登記我在我的ApplicationUncaughtExceptionHandleronCreate()方法,我通常是從我的用戶的文件。

我的錯誤日誌代碼如下:

我使用的應用程序的實現擴展Thread.UncaughtExceptionHandler。發生錯誤時,我會將例外信息寫入uncaughtException(Thread thread,Throwable ex)的日誌中。這是我找到的字符串打印:

// Get exception info 

    String newLine = "\r\n"; 
    String message = "Message: " + ex.getMessage(); 
    String cause = "Cause: " + ex.getCause(); 
    StackTraceElement[] stes = ex.getStackTrace(); 
    String stack; 

    // build stack trace 

    stack = "Stack: " + newLine; 

    for (int i = 0; i < stes.length; i++) 
     { 
     stack += stes[i].toString(); 
     stack += newLine; 
     } 

    // print out message 
    // print out cause 
    // print out stack 

此輸出到一個文件:

Message: Unable to create application com.(Modified app name for anonimity).*.*Application: java.lang.IllegalArgumentException Cause: java.lang.IllegalArgumentException Stack: android.app.ActivityThread.handleBindApplication(ActivityThread.java:4254) android.app.ActivityThread.access$1400(ActivityThread.java:140) android.app.ActivityThread$H.handleMessage(ActivityThread.java:1297) android.os.Handler.dispatchMessage(Handler.java:99) android.os.Looper.loop(Looper.java:137) android.app.ActivityThread.main(ActivityThread.java:4921) java.lang.reflect.Method.invokeNative(Native Method) java.lang.reflect.Method.invoke(Method.java:511) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) dalvik.system.NativeStart.main(Native Method)

附: 是的,我知道ACRA,但我現在不想使用它。

+0

對我來說也是同樣的問題。如果你知道解決方案,請幫助我。 – Sakthimuthiah 2013-06-07 13:38:28

+0

@Sakthimuthiah請參閱下面的答案。我根據公認的答案發布了我的最終解決方案。 – Jon 2013-06-07 16:22:26

回答

1

也許你的應用程序正在引發一個被捕獲然後重新生成的異常。

您只打印上一個異常的Stacktrace。也許這個例外有一個原因(ex.getCause()不是null)。在這種情況下,您還應該打印原因的堆棧跟蹤(可能是原因的原因..)。

你不必自己做。 Throwable有這個一個很好的方法,它打印出完整的堆棧跟蹤和回溯到PrintStream

public void printStackTrace(PrintStream s) 

之後,你只需要在printStream寫入日誌。

+0

'Throwable.printStackTrace()'創造了奇蹟。我沒有深入下去。謝謝! – Jon 2013-03-27 22:28:44

1

@micha仍然是公認的答案,但我想我會根據他的回答顯示我的修改方案。

 @Override 
public void uncaughtException(Thread thread, Throwable ex) 
    { 
    // This needs to be AIR TIGHT!. Do everything in a try-catch to prevent 
    // any exceptions from EVER occurring here. If an error does occur here 
    // then we will hang 
    // indefinitely and get an ANR. 
    try 
     { 
     FileManager fileMgr = FileManager.getInstance(myApp); 
     String logName = "ErrorLog.log"; 

     // print exception to byte array 

     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     PrintStream printer = new PrintStream(out); 
     ex.printStackTrace(printer); 

     // write out byte array as string to log file. 

     fileMgr.writeToLogFile(logName, out.toString(), false); 
     } 
    catch (Exception e) 
     { 
     } 

    // This allows the system to handle to exception and close the App. 

    defaultHandler.uncaughtException(thread, ex); 
    } 

我定製FileManager類完成實際寫入文件,但我用ThrowableprintStackTrace()得到完整的堆棧跟蹤,這是更深的話,我會對我自己完成的。

+0

非常感謝 – Sakthimuthiah 2013-06-12 12:46:29