报错日志 显示空指针异常
上日志
06-20 19:35:24.264 3122-3122/com.example.wangzhe.ssapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.wangzhe.ssapp, PID: 3122
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wangzhe.ssapp/com.example.wangzhe.ssapp.Drawer.Device_managment}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.lidroid.xutils.DbUtils com.example.wangzhe.ssapp.My.getDb()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.lidroid.xutils.DbUtils com.example.wangzhe.ssapp.My.getDb()' on a null object reference
at com.example.wangzhe.ssapp.Drawer.Device_managment.init(Device_managment.java:53)
at com.example.wangzhe.ssapp.Drawer.Device_managment.onCreate(Device_managment.java:40)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
getDb();
package com.example.wangzhe.ssapp;
import android.app.Application;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.DbUtils.DaoConfig;
public class My extends Application {
private static My mInstance;
private DbUtils db;
public static My getInstance() {
return mInstance;
}
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public DbUtils getDb() {
if (db == null) {
DaoConfig daoConfig = new DaoConfig(this);
daoConfig.setDbName("data.db");
daoConfig.setDbVersion(2);
db = DbUtils.create(daoConfig);
}
return db;
}
}