只有一個任務,爲什麼我的源碼的方法的命令,當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
請上傳'DatabaseOp'類的代碼 – Kedarnath
您可能想了解如何在調試模式下運行您的應用程序,並在代碼中設置斷點來檢查變量的值。 –
使用前初始化mDbHelper。 –