【发布时间】:2013-05-08 13:59:45
【问题描述】:
我试图让方法在当前活动之外的其他类中工作。在这种情况下,如果方法与活动在同一个类中,但每次调用都会出现 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)
【问题讨论】:
-
请发布堆栈跟踪并突出显示抛出异常的行。
-
Main的第 271 行是什么? -
@Egor 我添加了堆栈跟踪,但我不知道如何突出显示行。它在“bckp.copyBackup(backupdir, backupdirfile, database, context);”上的 Main.class 中抛出
标签: android class android-activity nullpointerexception android-context