2014-04-22 49 views
1

只有一個任務,爲什麼我的源碼的方法的命令,當IAM從服務調用它寫我一個NullPointerException? ...當我嘗試使用吐司,它工作完美,但在我使用mDbHelper.open/naplnSviatky/close後,我的應用程序將崩潰。的Android,NullPointerException異常的方法,在服務SQLite的命令

這方法只有查詢命令,並在活動和片段工作正常

這裏是我的service

public class SviatokFullFillService extends Service 
{ 
String username; 
private DatabaseOp mDbHelper; 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public void onStart(Intent intent, int startId) 
{ 
    // TODO Auto-generated method stub 
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); 

    username = sharedPrefs.getString("prefUsername", "NULL"); 

    //Toast.makeText(this, username, Toast.LENGTH_SHORT).show(); 
    mDbHelper.open(); 
    mDbHelper.naplnSviatky(username); 
    mDbHelper.close(); 
} 

} 

這裏是我的LogCat

04-22 14:16:43.890: E/AndroidRuntime(6560): FATAL EXCEPTION: main 
04-22 14:16:43.890: E/AndroidRuntime(6560): java.lang.RuntimeException: Unable to start service [email protected] with Intent { cmp=sk.tomino.studentDiary/.SviatokFullFillService }: java.lang.NullPointerException 
04-22 14:16:43.890: E/AndroidRuntime(6560):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2950) 
04-22 14:16:43.890: E/AndroidRuntime(6560):  at android.app.ActivityThread.access$1900(ActivityThread.java:151) 
04-22 14:16:43.890: E/AndroidRuntime(6560):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442) 
04-22 14:16:43.890: E/AndroidRuntime(6560):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-22 14:16:43.890: E/AndroidRuntime(6560):  at android.os.Looper.loop(Looper.java:155) 
04-22 14:16:43.890: E/AndroidRuntime(6560):  at android.app.ActivityThread.main(ActivityThread.java:5520) 
04-22 14:16:43.890: E/AndroidRuntime(6560):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-22 14:16:43.890: E/AndroidRuntime(6560):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-22 14:16:43.890: E/AndroidRuntime(6560):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 
04-22 14:16:43.890: E/AndroidRuntime(6560):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796) 
04-22 14:16:43.890: E/AndroidRuntime(6560):  at dalvik.system.NativeStart.main(Native Method) 
04-22 14:16:43.890: E/AndroidRuntime(6560): Caused by: java.lang.NullPointerException 
04-22 14:16:43.890: E/AndroidRuntime(6560):  at sk.tomino.studentDiary.SviatokFullFillService.onStart(SviatokFullFillService.java:30) 
04-22 14:16:43.890: E/AndroidRuntime(6560):  at android.app.Service.onStartCommand(Service.java:450) 
04-22 14:16:43.890: E/AndroidRuntime(6560):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2916) 
04-22 14:16:43.890: E/AndroidRuntime(6560):  ... 10 more 
+0

請上傳'DatabaseOp'類的代碼 – Kedarnath

+0

您可能想了解如何在調試模式下運行您的應用程序,並在代碼中設置斷點來檢查變量的值。 –

+0

使用前初始化mDbHelper。 –

回答

1

你必須在類級別聲明變量private DatabaseOp mDbHelper;但是當您在onStart()方法中使用它時,您尚未初始化它,因此這是投擲NullPointerException

+0

好的謝謝,我忘了它...請,你可以幫我在背景上玩naplnSviatok方法,我開始後,我的主線將等待並凍結應用程序,我需要啓動這個方法和gui下一步 – Tomino

+0

@Tomino該方法在哪裏? – Kedarnath

+0

in databaseOp.class – Tomino

相關問題