无法从数据库获取数据用户

时间:2017-08-15 15:19:23

标签: android ormlite

我使用Ormlite来使用database.I写函数搜索用户firebaseId。 此功能获取有关用户的信息:全名,电子邮件,照片。但是当我去做个人资料活动时,我的请求会停止

ProfileActivity.class

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ButterKnife.bind(this);

        dbHelper = new DbHelper(this);

        Application application = (Application) getApplicationContext();
        userFirebaseId = application.getFirebaseId();
//show line error 
        userMy = dbHelper.getByUser(userFirebaseId);

        nameEditText.setText(userMy.getFullName());
        emailEditText.setText(userMy.getEmail());


        auth = FirebaseAuth.getInstance();
        user = FirebaseAuth.getInstance().getCurrentUser();
        authListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (user == null) {
                    startActivity(new Intent(ProfileActivity.this, LoginActivity.class));
                    finish();
                }
            }
        };
    }

DbHepler.class

public class DbHelper扩展了OrmLiteSqliteOpenHelper {         PreparedQuery pq;

    Dao<User, ?> userDao=null;
    Dao<MyDream, ?> myDreamDao = null;

    public static final String DB_NAME = "dream.db";
    public static final int DB_VERSION = 1;

    public DbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {

        try {
            TableUtils.createTable(connectionSource, Dream.class);
            TableUtils.createTable(connectionSource, User.class);
            TableUtils.createTable(connectionSource, MyDream.class);
            Dao<Dream, Integer> dreamDao = getDao(Dream.class);
            userDao = getDao(User.class);
            myDreamDao = getDao(MyDream.class);
             } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            TableUtils.dropTable(connectionSource, Dream.class, true);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public User getByUser(String firebaseId) {
// this error line 
        QueryBuilder<User, ?> qb = userDao.queryBuilder();
        try {
            qb.where().eq(User.Columns.FIREBASE_ID, firebaseId);
        } catch (SQLException e) {
            e.printStackTrace();
        }


        try {
            pq = qb.prepare();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        try {
            return userDao.queryForFirst(pq);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    }

我的日志

FATAL EXCEPTION: main
Process: magdalena.pl.dreamguide, PID: 11813
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{magdalena.pl.dreamguide/magdalena.pl.dreamguide.activity.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'com.j256.ormlite.stmt.QueryBuilder com.j256.ormlite.dao.Dao.queryBuilder()' 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 interface method 'com.j256.ormlite.stmt.QueryBuilder com.j256.ormlite.dao.Dao.queryBuilder()' on a null object reference
                                                                         at magdalena.pl.dreamguide.db.DbHelper.getByUser(DbHelper.java:460)
                                                                         at magdalena.pl.dreamguide.activity.ProfileActivity.onCreate(ProfileActivity.java:99)
                                                                         at android.app.Activity.performCreate(Activity.java:6251)
                                                                         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) 

1 个答案:

答案 0 :(得分:0)

您在userDao方法中初始化onCreate。仅在按照here解释创建数据库时才调用此方法。因此,初始化userDao的代码应该更接近其使用,或者在使用之前执行的代码。