【问题标题】:Android pass context causes NullPointerExceptionAndroid 传递上下文导致 NullPointerException
【发布时间】: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


【解决方案1】:

你还没有初始化bckp,所以当你尝试调用它的函数时它是null。你的context 很好

Backup bckp;

做起来

Backup bckp = new Backup();  // send parameters if your constructor takes them
private Context context = Main.this;
bckp.copyBackup(backupdir, backupdirfile, database, context);

【讨论】:

  • 哦...当然...我什至在相同的代码中也有一个示例,但是 Eclipse 要求在初始化的类中提供构造函数,这让我很恼火。无论如何,它现在有效。非常感谢。
  • 不客气。顺便说一句,在您的 logcat 中引用您的代码的异常之后的第一行是开始的地方。这就是我如何知道它在那里并寻找一个正在执行功能的对象
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-17
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多