0

我有一个带有列表视图的活动,它显示使用 SDCARD 图像注册的产品。一些用户报告说他们在进行此活动时收到错误“应用程序已关闭”。

我已经做了所有的测试来尝试模拟它,并且没有发生错误。我认为这可能与设备内存或其他东西有关。

4

2 回答 2

0

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

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() 方法有示例用法。

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().
于 2013-09-24T22:27:01.493 回答
0

Android检测到您的应用程序在3-5秒内没有加载或响应时,大多数情况下都会出现“应用程序关闭”错误,您应该尝试检查SD卡的可用性、可用空间、权限甚至运行某些进程在后台。重写你的代码来处理这些情况,它应该可以工作。

于 2013-09-25T02:14:16.957 回答