我有一种方法尝试写入 AlarmReceiver 中 SD 卡上的文本文件。在我将它移到 AlarmReceiver 之前,该方法运行良好,但现在它在 Logcat 中因以下内容而崩溃:
09-11 10:36:44.395: D/AndroidRuntime(946): Shutting down VM
09-11 10:36:44.395: W/dalvikvm(946): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
09-11 10:36:44.425: E/AndroidRuntime(946): FATAL EXCEPTION: main
09-11 10:36:44.425: E/AndroidRuntime(946): java.lang.RuntimeException: Unable to start receiver com.exercise.AndroidSYSinfo.AlarmReceiver: java.lang.NullPointerException
09-11 10:36:44.425: E/AndroidRuntime(946): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2126)
09-11 10:36:44.425: E/AndroidRuntime(946): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
09-11 10:36:44.425: E/AndroidRuntime(946): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197)
09-11 10:36:44.425: E/AndroidRuntime(946): at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 10:36:44.425: E/AndroidRuntime(946): at android.os.Looper.loop(Looper.java:137)
09-11 10:36:44.425: E/AndroidRuntime(946): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-11 10:36:44.425: E/AndroidRuntime(946): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 10:36:44.425: E/AndroidRuntime(946): at java.lang.reflect.Method.invoke(Method.java:511)
09-11 10:36:44.425: E/AndroidRuntime(946): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-11 10:36:44.425: E/AndroidRuntime(946): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-11 10:36:44.425: E/AndroidRuntime(946): at dalvik.system.NativeStart.main(Native Method)
09-11 10:36:44.425: E/AndroidRuntime(946): Caused by: java.lang.NullPointerException
09-11 10:36:44.425: E/AndroidRuntime(946): at java.io.Writer.write(Writer.java:141)
09-11 10:36:44.425: E/AndroidRuntime(946): at com.exercise.AndroidSYSinfo.AlarmReceiver.writeSD(AlarmReceiver.java:318)
09-11 10:36:44.425: E/AndroidRuntime(946): at com.exercise.AndroidSYSinfo.AlarmReceiver.onReceive(AlarmReceiver.java:182)
09-11 10:36:44.425: E/AndroidRuntime(946): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2119)
这是相关代码:
public void writeSD(Context context)
{
//Toast.makeText(context,"Saving sdcardstats.txt..." , Toast.LENGTH_SHORT).show();
//
// Write SDCard stats to file
//
try
{
FileOutputStream fOut1 = context.openFileOutput("sdcardstats.txt", Context.MODE_PRIVATE);
OutputStreamWriter osw1 = new OutputStreamWriter(fOut1);
osw1.write(string);
osw1.flush();
osw1.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
} //end catch
} // end of writeSD()