我想使方法从除当前活动以外的其他类工作。在这种情况下,有一种备份机制,如果该方法与活动在相同的类中,但有时会使用Toast并且完美地工作,但每次调用时都会有NullPointerException。Android传递上下文导致NullPointerException
这些都是从主活动的基本线:
Backup bckp;
private Context context = Main.this;
bckp.copyBackup(backupdir, backupdirfile, database, context);
和调用的方法:
public void copyBackup(final String backupdir,
final String target, final String source, final Context context) {
final File sdcard = Environment.getExternalStorageDirectory();
if (sdcard.canWrite()) {
InputStream input = null;
try {
input = new FileInputStream(source);
} catch (FileNotFoundException e) {
Toast.makeText(context, "There was an error finding the database", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
File dir = new File (backupdir);
if (!dir.exists()) {
dir.mkdirs();
}
OutputStream output = null;
try {
output = new FileOutputStream(target);
} catch (FileNotFoundException e) {
Toast.makeText(context, "There was an error creating the backup", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
byte[] buffer = new byte[1024];
int length;
try {
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} catch (IOException e) {
Toast.makeText(context, "There was an error copying the database", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
try {
output.flush();
output.close();
input.close();
} catch (IOException e) {
Toast.makeText(context, "There was an error ending the copy", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
} else {
Toast.makeText(context, "No SD card found", Toast.LENGTH_SHORT).show();
}
}
这是没有意义的,我为什么它的工作原理,如果所有的都在一个班,但不如果它是在一个单独的类... 感谢您的帮助!
编辑:
05-08 14:13:24.935: E/AndroidRuntime(636): FATAL EXCEPTION: main
05-08 14:13:24.935: E/AndroidRuntime(636): java.lang.NullPointerException
05-08 14:13:24.935: E/AndroidRuntime(636): at maturaarbeit.nicola_pfister.marks.Main$9$1.onClick(Main.java:271)
05-08 14:13:24.935: E/AndroidRuntime(636): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
05-08 14:13:24.935: E/AndroidRuntime(636): at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 14:13:24.935: E/AndroidRuntime(636): at android.os.Looper.loop(Looper.java:137)
05-08 14:13:24.935: E/AndroidRuntime(636): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-08 14:13:24.935: E/AndroidRuntime(636): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 14:13:24.935: E/AndroidRuntime(636): at java.lang.reflect.Method.invoke(Method.java:511)
05-08 14:13:24.935: E/AndroidRuntime(636): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-08 14:13:24.935: E/AndroidRuntime(636): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-08 14:13:24.935: E/AndroidRuntime(636): at dalvik.system.NativeStart.main(Native Method)
请张贴堆栈跟踪,并着重异常被抛出的线。 – Egor 2013-05-08 14:01:28
“Main”的第271行是什么? – codeMagic 2013-05-08 14:17:09
@Egor我添加了堆栈跟踪,但我不知道如何突出显示线条。它被放在Main.class的“bckp.copyBackup(backupdir,backupdirfile,database,context);” – BlueHazard 2013-05-08 14:18:50