2013-09-24 185 views
0

我有一个列表视图的活动,显示向SDCARD的图像注册的产品。有些用户报告说,他们在此活动中收到错误“应用程序已关闭”。Android:应用程序意外关闭

我已经完成了所有的测试,试图模拟它,并且没有发生错误。 我认为这可能是设备内存或东西的东西。

回答

0

这是我很久以前开发的一个类,用于捕获我分发给几个朋友的jar文件中的错误。它捕获输出到标准输出和标准错误并将其写入文件。 如果您的用户可以使用文件管理器查找写入的文件,则可以将其发送给您。我已经对一个应用程序进行了一些初步测试,它对我很有帮助。
如果您使用它并遇到问题,请告诉我。

package com.normstools; 


import java.io.*; 

//------------------------------------------------------------------------ 
public class SaveStdOutput extends PrintStream { 
    final static boolean debug = false;  // controls debug output 

    static OutputStream logfile; 
    static PrintStream oldStdout = null; 
    static PrintStream oldStderr = null; 

    private boolean echoOutput = true;  //Also output to old setting 

    // Constructor - we're the only one that can use it! 
    private SaveStdOutput(PrintStream ps, boolean echoOutput) { 
     super(ps); 
    this.echoOutput = echoOutput; 
//  System.out.println("SaveStdOutput constructor called"); 
    } // end Constructor 

    //------------------------------------------------------------ 
    // Starts copying stdout and stderr to the file f. 
    public static void start(String f) throws IOException { 
     // Create/Open logfile. 
     OutputStream os = new PrintStream(
        new BufferedOutputStream(
         new FileOutputStream(f, true))); // append to current 
     doCommon(os, true);   
} // end start() 

    // Copy STDOUT and STDERR to an output stream 
    public static void start(OutputStream os) { 
     doCommon(os, true); 
    } // end start() 
    public static void start(OutputStream os, boolean eO) { 
     doCommon(os, eO); 
    } // end start() 

    //------------------------------------------------------- 
// Finish up 
private static void doCommon(OutputStream os, boolean echoOutput) { 
     // Only allow to be called once 
     if (oldStdout != null) { 
      if (debug) 
       System.err.println("SaveStdOutput start() called twice"); 
      return;     // Exit if already open 
     } 
    logfile = os; 
    // Save old settings. 
    oldStdout = System.out; 
    oldStderr = System.err; 

    // Start redirecting the output. 
    System.setOut(new SaveStdOutput(System.out, echoOutput)); 
    System.setErr(new SaveStdOutput(System.err, echoOutput)); 
    } // end doCommon() 

    //-------------------------------------- 
    // Restores the original settings. 
    public static void stop() { 
     if (oldStdout == null) { 
      if (debug) 
       System.err.println("SaveStdOutput stop() called before start()"); 
      return; 
     } 
     System.setOut(oldStdout); 
     oldStdout = null;    //Clear 
     System.setErr(oldStderr); 
     try { 
      logfile.close(); 
     } catch (Exception ex) { 
      System.err.println("SaveStdOutput stop() ex " + ex.getMessage()); 
      ex.printStackTrace(); 
     } 
    } // end stop() 

    // Override the PrintStream write methods 
    public void write(int b) { 
     try { 
      logfile.write(b); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      setError(); 
     } 
    if (echoOutput) 
     super.write(b); 
    } // end write() 

    // PrintStream override. 
    public void write(byte buf[], int off, int len) { 
     try { 
      logfile.write(buf, off, len); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      setError(); 
     } 
    if (echoOutput) 
     super.write(buf, off, len); 
    } // end write() 

    //------------------------------------------------------------------- 
    // Following for testing SaveStdOutput class: Comment out when done! 
    public static void main(String[] args) { 
     try { 
      // Start capturing characters into the log file. 
      SaveStdOutput.start("log.txt"); 

      // Test it. 
      System.out.println("Here's is some stuff to stdout. " 
            + new java.util.Date()); 
      System.err.println("Here's is some stuff to stderr."); 
      System.out.println("Let's throw an exception..."); 
      new Exception().printStackTrace(); 
      throw new Exception("this is thrown"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      // Stop capturing characters into the log file 
      // and restore old setup. 
      SaveStdOutput.stop(); 
     } 
     System.out.println("This should be to console only!"); 
    } // end main() */ 
} // end class SaveStdOutput 

main()方法有示例用法。当检测的Android应用程序没有加载或响应在3-5秒,你应该尝试检查SD卡的可用性,可用空间

Call the start() method in onStart() and the close() method in onStop(), 
or add menu items to control it. Add a few calls to System.out.println() 
and some try{}catch blocks with printStackTrace(). 
0

错误“应用程序关闭”时的大部分时间,许可甚至在后台运行一些进程。 重写你的代码来处理这些情况它应该工作。