Android:应用程序意外关闭

时间:2013-09-24 21:36:34

标签: android android-activity

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

我已经完成了所有测试以尝试模拟它,并且没有发生错误。 我认为它可能与设备内存有关。

2 个答案:

答案 0 :(得分: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().

答案 1 :(得分:0)

错误“关闭应用程序”大部分时间是 Android检测到您的应用程序未加载或响应,在3-5秒内,您应该尝试检查SD卡的可用性,空间可用,允许甚至在后台运行某些过程。 重写代码以处理这些情况,它应该可以正常工作。