0

我想使方法從除當前活動以外的其他類工作。在這種情況下,有一種備份機制,如果該方法與活動在相同的類中,但有時會使用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) 
+0

請張貼堆棧跟蹤,並着重異常被拋出的線。 – Egor 2013-05-08 14:01:28

+0

「Main」的第271行是什麼? – codeMagic 2013-05-08 14:17:09

+0

@Egor我添加了堆棧跟蹤,但我不知道如何突出顯示線條。它被放在Main.class的「bckp.copyBackup(backupdir,backupdirfile,database,context);」 – BlueHazard 2013-05-08 14:18:50

回答

2

您還沒有初始化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); 
+0

Oooh ......當然......我甚至在代碼中都有一個例子,但是Eclipse被初始化類中的構造函數激怒了。無論如何它現在有效。非常感謝。 – BlueHazard 2013-05-08 14:23:37

+0

非常歡迎。順便說一句,您的logcat中引用您的代碼的異常之後的第一行是開始的地方。這就是我知道它在那裏,並尋找一個有一個功能正在執行的對象 – codeMagic 2013-05-08 14:26:07

相關問題