wangzhe1994
wangzhe1994
采纳率0%
2016-06-20 19:49 阅读 1.6k

android运行报错求教 空指针异常

报错日志 显示空指针异常
上日志
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;
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • niaonao niaonao 2016-06-20 23:19

    getInstance();
    getDb();
    都是 My的方法
    my.getInstance().getDb();,同时调用两个方法不对,前一个方法已经返回了结果,不就结束了;
    两个方法要分开写,或者用不同对象来调用,调用时也要分开。

    点赞 评论 复制链接分享

相关推荐