【问题标题】:Why am I receiving a "thread exiting with uncaught exception”?为什么我会收到“线程退出但未捕获的异常”?
【发布时间】:2013-08-08 18:55:21
【问题描述】:

我正在使用 Parse.com 进行推送通知。当我收到推送通知时,这个类会执行:

public class MyCustomReceiver extends BroadcastReceiver {

    protected ObjetoMensaje DatosObjecto;
    protected SerializacionDeDatos Sdd;
    protected String alert, fecha, name, tipo;
    private static final String TAG = "MyCustomReceiver";

  @Override
  public void onReceive(Context context, Intent intent) {
    try {

        DatosObjecto = new ObjetoMensaje();
        Sdd = new SerializacionDeDatos();

      String action = intent.getAction();
      String channel = intent.getExtras().getString("com.parse.Channel");
      JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

      Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
      Iterator<?> itr = json.keys();
      Log.i("","");

      while (itr.hasNext()) {
        String key = (String) itr.next();
        Log.d(TAG, "..." + key + " => " + json.getString(key));
        Log.d(TAG,"");
       }

      alert = json.getString("alert").toString();
      name = json.getString("name").toString();
      tipo = json.getString("tipo").toString();

      DatosObjecto.setAlert(alert);
      DatosObjecto.setName(name);
      DatosObjecto.setTipo(tipo);

      Sdd.Serializa(DatosObjecto); //this line, I use for call the class "SerializacionDeDatos"

    } catch (JSONException e) {
      Log.d(TAG, "JSONException: " + e.getMessage());
    }
  }
}

这些行:

 alert = json.getString("alert").toString();
  name = json.getString("name").toString();
  tipo = json.getString("tipo").toString();

  DatosObjecto.setAlert(alert);
  DatosObjecto.setName(name);
  DatosObjecto.setTipo(tipo);

当我收到推送时,我正在提取“alert”、“name”和“tipo”的值。我把它们放在一个 ObjetoMensaje 对象中。代码:

public class ObjetoMensaje extends Activity implements Serializable{ 

private static final long serialVersionUID = 5680898935329497057L; 
private String  alert, name, tipo; 
protected String filename = "datos.dat";

public ObjetoMensaje(){}; 

public ObjetoMensaje(String alert, String name, String tipo){ 
    super(); 
    this.alert = alert;
    this.name = name;
    this.tipo = tipo; 
    }

public String getAlert(){
    return alert;
}

public void setAlert(String alert){
    this.alert = alert;
    Log.i("Set Alert", "Excitoso");
}

public String getName(){
    return name;
}

public void setName(String name){
    this.name = name;
    Log.i("Set Name", "Excitoso");
}

public String getTipo(){
    return tipo;
}

public void setTipo(String tipo){
    this.tipo = tipo;
    Log.i("Set tipo", "Excitoso");
}
}

我想序列化值“alert”、“name”和“tipo”,所以我创建了一个序列化类:

public class SerializacionDeDatos extends Activity{

protected String filename = "datos.dat";
protected void Serializa(ObjetoMensaje DatosObjecto){
            FileOutputStream fos;
            try {
                fos = openFileOutput(filename, Context.MODE_PRIVATE);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(DatosObjecto);
                oos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } 
            catch (IOException e) {
                e.printStackTrace();
            }
    }
}

当我调用类时,我收到此错误:

08-08 13:15:32.976: W/dalvikvm(8360): threadid=1: thread exiting with uncaught exception (group=0x4001c578)
08-08 13:15:33.070: E/AndroidRuntime(8360): FATAL EXCEPTION: main
08-08 13:15:33.070: E/AndroidRuntime(8360): java.lang.RuntimeException: Unable to start receiver mx.nivel9.apps.MyCustomReceiver: java.lang.NullPointerException
08-08 13:15:33.070: E/AndroidRuntime(8360):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1809)
08-08 13:15:33.070: E/AndroidRuntime(8360):     at android.app.ActivityThread.access$2400(ActivityThread.java:117)
08-08 13:15:33.070: E/AndroidRuntime(8360):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
08-08 13:15:33.070: E/AndroidRuntime(8360):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 13:15:33.070: E/AndroidRuntime(8360):     at android.os.Looper.loop(Looper.java:130)
08-08 13:15:33.070: E/AndroidRuntime(8360):     at android.app.ActivityThread.main(ActivityThread.java:3687)
08-08 13:15:33.070: E/AndroidRuntime(8360):     at java.lang.reflect.Method.invokeNative(Native Method)
08-08 13:15:33.070: E/AndroidRuntime(8360):     at java.lang.reflect.Method.invoke(Method.java:507)
08-08 13:15:33.070: E/AndroidRuntime(8360):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-08 13:15:33.070: E/AndroidRuntime(8360):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-08 13:15:33.070: E/AndroidRuntime(8360):     at dalvik.system.NativeStart.main(Native Method)
08-08 13:15:33.070: E/AndroidRuntime(8360): Caused by: java.lang.NullPointerException
08-08 13:15:33.070: E/AndroidRuntime(8360):     at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
08-08 13:15:33.070: E/AndroidRuntime(8360):     at mx.nivel9.apps.SerializacionDeDatos.Serializa(SerializacionDeDatos.java:23)
08-08 13:15:33.070: E/AndroidRuntime(8360):     at mx.nivel9.apps.MyCustomReceiver.onReceive(MyCustomReceiver.java:50)
08-08 13:15:33.070: E/AndroidRuntime(8360):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1798)
08-08 13:15:33.070: E/AndroidRuntime(8360):     ... 10 more

我做错了什么?

【问题讨论】:

    标签: android serialization parse-platform


    【解决方案1】:

    错误来自您在 SerializacionDeDatos 类中的代码行 23。 您正在对尚未初始化的对象调用方法 - 这意味着您为对象创建了一个变量,但没有使用“new”运算符创建对象或初始化返回 null。

    如果“文件名”无效,这行可能是问题。

    fos = openFileOutput(filename, Context.MODE_PRIVATE);
    

    幸运的是,您的代码 sn-p 中没有行号,所以我无法确定错误的确切来源。

    【讨论】:

      【解决方案2】:

      您的异常日志中的这些行:

      08-08 13:15:33.070: E/AndroidRuntime(8360):     at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
      08-08 13:15:33.070: E/AndroidRuntime(8360):     at mx.nivel9.apps.SerializacionDeDatos.Serializa(SerializacionDeDatos.java:23)
      

      告诉我们异常被抛出

       fos = openFileOutput(filename, Context.MODE_PRIVATE);
      

      SerializacionDeDatos 类中,或者更明显的是对openFileOutput 的调用,这意味着您所引用的文件可能存在根本问题(权限、存在等)

      除非您针对每种异常类型执行特定的操作,否则您应该在您的 Serializa 方法中为 (Exception e) 添加一个通用的 catch 到您的 try,并打印堆栈跟踪以找出是什么错误如下:

              try {
      
                  fos = openFileOutput(filename, Context.MODE_PRIVATE);
                  ObjectOutputStream oos = new ObjectOutputStream(fos);
                  oos.writeObject(DatosObjecto);
                  oos.close();
      
              } catch (Exception e) {
      
                  e.printStackTrace();
              } 
      

      进一步注意,您没有关闭您的 FileOutputStream 最佳做法是在 try 之外使用 null 对其进行初始化,然后在您的 finally 中调用 close() 方法块,如下:

              FileOutputStream fos = null;
              ObjectOutputStream oos = null;
      
              try {
      
                  fos = openFileOutput(filename, Context.MODE_PRIVATE);
                  oos = new ObjectOutputStream(fos);
                  oos.writeObject(DatosObjecto);
      
              } catch (Exception e) {
      
                  e.printStackTrace(); // NOW this should now tell us what's going wrong.
      
              } finally {
      
                  try {
      
                      oos.close();
      
                  } catch (Exception e) {
      
                      e.printStackTrace();
                  }
      
                  try {
      
                      fos.close();
      
                  } catch (Exception e) {
      
                      e.printStackTrace();
                  }
              }
      

      更新:您在下面的 cmets 中注意到您收到了 NullPointerException。这是调用Activity.openFileOutput() 时非常常见的问题。

      检查您的答案是否在这些链接中:

      【讨论】:

      • 我照你说的扔了很多system.err,前三个是:W/System.err(10893): java.lang.NullPointerException W/System.err(10893): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158) W/System.err(10893): at mx.nivel9.apps.SerializacionDeDatos.Serializa(SerializacionDeDatos.java:23)
      • 您对Activity.openFileOutput 的调用正在抛出NullPointerException。这是一个常见的问题。检查您的答案是否在这里:stackoverflow.com/questions/10259421/… 或这里stackoverflow.com/questions/6282313/…
      • 我不需要 IOException 和 FileNotFoundException?
      • 这是您的电话。基本 Java 101。如果您需要区分如何处理 IOException 和如何处理 FileNotFoundException,则区分,否则,两者都扩展 Exception,因此它们将被捕获。
      • 您仍然需要解决根本问题:为什么这个方法openFileOutput 会抛出NullPointerException。您应该编辑您的原始问题并包含您收到的整个 NEW 堆栈跟踪。
      猜你喜欢
      • 1970-01-01
      • 2016-02-09
      • 2023-03-15
      • 2013-04-11
      • 2017-10-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多