我正在使用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);


收到请求后,我将提取“警报”,“名称”和“技巧”的值。我将它们放在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


我究竟做错了什么?

最佳答案

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

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的调用,这意味着您所引用的文件可能存在根本问题(权限,存在等)。

除非您针对每种类型的Exception做特定的事情,否则您应该只将catch的常规(Exception e)添加到try方法中的Serializa中,并打印堆栈跟踪以找出问题所在,如下所示:

        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,然后在close()块中调用finally方法,如下所示:

        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();
            }
        }




更新:您已在下面的注释中指出您收到了NullPointerException。调用Activity.openFileOutput()时,这是一个非常常见的问题。

检查您的答案是否在以下任何链接中:


NullPointerException at openFileOutput in Activity
NullPointer exception in ContextWrapper
Unable to start activity caused by NullPointerException at ContextImpl.openFileOutput
android what is wrong with openFileOutput?
null pointer in openFileOutput

10-08 09:13