Android传递上下文导致NullPointerException

Android传递上下文导致NullPointerException,android,class,android-activity,nullpointerexception,android-context,Android,Class,Android Activity,Nullpointerexception,Android Context,我试图使方法在当前活动之外的其他类中工作。在这种情况下,有一种备份机制,它有时使用toasts,如果该方法与活动在同一个类中,但每次调用它时都有一个NullPointerException,则该机制可以完美地工作 以下是主要活动的基本要点: Backup bckp; private Context context = Main.this; bckp.copyBackup(backupdir, backupdirfile, database, context); 以及被调用的方法: public

我试图使方法在当前活动之外的其他类中工作。在这种情况下,有一种备份机制,它有时使用toasts,如果该方法与活动在同一个类中,但每次调用它时都有一个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();
    }
}
这对我来说毫无意义,为什么它在一个类中有效,但在一个单独的类中无效。。。 谢谢你的帮助

编辑:


您尚未初始化
bckp
,因此当您尝试对其调用函数时,它是
null
。您的
上下文
很好

Backup bckp;
成功

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

请发布stacktrace并突出显示引发异常的行。
Main
的第271行是什么?@Egor我添加了stacktrace,但我不知道如何突出显示行。它被抛出到Main.class中的“bckp.copyBackup(backupdir,backupdirfile,database,context);”ooh。。。当然我甚至在同一个代码中有一个例子,但Eclipse要求在初始化的类中使用构造函数,这让我很恼火。不管怎么说,它现在起作用了。非常感谢。不客气。顺便说一下,logcat中引用代码的异常后的第一行是开始的地方。这就是为什么我知道它在那里,并寻找一个对象,它有一个功能正在执行
Backup bckp = new Backup();  // send parameters if your constructor takes them
private Context context = Main.this;
bckp.copyBackup(backupdir, backupdirfile, database, context);