在过去的三天里,我无法理解所面临的问题。我得到的是一些变量的空值,而不是其他变量的空值,有时是所有变量的空值。在运行棒棒糖的三星设备上,这一问题尤其突出。
实际上,我所做的是在我的loginactivity中通过sqlitehandler类存储服务器返回的用户数据,然后在以后使用mainactivity中的数据。sqlitehandler随机返回空值,因此我无法确定数据库是否在loginactivity中正确写入,或者在读取时是否存在访问问题。我正在发布loginactivity和sqlitehandler类的相关部分。请帮忙。
@Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response);
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
// Now store the user in SQLite
String api_key = jObj.getString("api_key");
//JSONObject user = jObj.getJSONObject("user");
String name = jObj.getString("name");
String email = jObj.getString("email");
int user_id = jObj.getInt("user_id");
JSONObject authorization = jObj.getJSONObject("authorizations");
int pro_app = authorization.getInt("pro_app");
// Inserting row in users table
// SQLite database handler
db = new SQLiteHandler(LoginActivity.this);
db.addUser(name, email, user_id, api_key);
db.addAuthorizations(pro_app);
// Set the log level to verbose.
String user_id_string = String.valueOf("user_id");
String pro_app_string = String.valueOf("pro_app");
session.setLogin(true);
Toast.makeText(LoginActivity.this, "You have successfully logged in! Please wait while application loads.", Toast.LENGTH_LONG).show();
// Launch main activity
Intent intent = new Intent(LoginActivity.this,
SplashActivity.class);
startActivity(intent);
finish();
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("message");
if (errorMsg.equals("") || errorMsg.isEmpty()) {
errorMsg = "An unusual error occurred. Please try again";
}
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
// JSON error
hideDialog();
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
以下是sqlitehandler:
public class SQLiteHandler extends SQLiteOpenHelper {
private static final String TAG = SQLiteHandler.class.getSimpleName();
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "android_api";
// Login table name
private static final String TABLE_USER = "user";
private static final String TABLE_AUTH = "authorizations";
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_USER_ID = "user_id";
private static final String KEY_API = "api_key";
private static final String KEY_PRO = "pro_app";
public SQLiteHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE," + KEY_USER_ID + " INTEGER UNIQUE," + KEY_API + " TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
String CREATE_AUTH_TABLE = "CREATE TABLE " + TABLE_AUTH + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_PRO + " INTEGER"
+ ")";
db.execSQL(CREATE_AUTH_TABLE);
Log.d(TAG, "Database tables created");
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(TAG, "onUpgrade() from " + oldVersion + " to " + newVersion);
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_AUTH);
// Create tables again
onCreate(db);
}
/**
* Storing user details in database
*/
public void addUser(String name, String email, int user_id, String c_code, String phone, String api_key, int status, String created_at) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name); // Name
values.put(KEY_EMAIL, email); // Email
values.put(KEY_USER_ID, user_id); // Email
values.put(KEY_API, api_key); // API
// Inserting Row
long id = db.insert(TABLE_USER, null, values);
db.close(); // Closing database connection
Log.d(TAG, "New user inserted into sqlite: " + id);
}
public void addAuthorizations(int pro_app, int no_ads, int pro_original) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PRO, pro_app); // Pro version?
// Inserting Row
long id = db.insert(TABLE_AUTH, null, values);
db.close(); // Closing database connection
Log.d(TAG, "New auth inserted into sqlite: " + id);
}
public void updateAuthorizations(int pro_app, int no_ads, int pro_original) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PRO, pro_app); // Pro version?
values.put(KEY_ADS, no_ads); // No Ads?
values.put(KEY_PRO_ORIGINAL, pro_original); //pro origin
// Inserting Row
db.update(TABLE_AUTH, values, "id=1", null);
db.close(); // Closing database connection
Log.d(TAG, "New auth updated into sqlite ");
}
/**
* Getting user data from database
*/
public HashMap<String, Object> getUserDetails() {
HashMap<String, Object> user = new HashMap<String, Object>();
String selectQuery = "SELECT * FROM " + TABLE_USER;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("user_id", cursor.getInt(3));
user.put("api_key", cursor.getString(4));
}
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
return user;
}
public HashMap<String, Integer> getUserAuthorizations() throws Exception{
HashMap<String, Integer> auth = new HashMap<String, Integer>();
String selectQuery = "SELECT * FROM " + TABLE_AUTH;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
auth.put("pro_app", cursor.getInt(1));
}
cursor.close();
db.close();
// return user
try {
int authINT = auth.get("pro_app");
} catch (Exception e){
Crashlytics.log("this is what is throwing the error");
Crashlytics.logException(e);
}
int authINTNEXT = auth.get("pro_app");
return auth;
}
/**
* Re crate database Delete all tables and create them again
*/
public void deleteUsers() {
SQLiteDatabase db = this.getWritableDatabase();
// Delete All Rows
db.delete(TABLE_USER, null, null);
db.delete(TABLE_AUTH, null, null);
db.close();
Log.d(TAG, "Deleted all user info from sqlite");
}
}
我进入Crashlytics时出错:
Non-fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
raw
com.blueinklabs.investifystocks.helper.SQLiteHandler.getUserAuthorizations (SourceFile:198)
com.blueinklabs.investifystocks.StorageMethod.loadDatabaseData (SourceFile:65)
com.blueinklabs.investifystocks.MainActivity.onCreate (SourceFile:183)
android.app.Activity.performCreate (Activity.java:5993)
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1111)
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2418)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2530)
android.app.ActivityThread.access$900 (ActivityThread.java:163)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1358)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:135)
android.app.ActivityThread.main (ActivityThread.java:5536)
java.lang.reflect.Method.invoke (Method.java)
java.lang.reflect.Method.invoke (Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1397)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1192)
最佳答案
实际上问题是Cursor
是从0位置返回的数据,这意味着您得到了记录0,1,2。态度。检查下面的MU代码
/**
* Getting user data from database
*/
public HashMap<String, Object> getUserDetails() {
HashMap<String, Object> user = new HashMap<String, Object>();
String selectQuery = "SELECT * FROM " + TABLE_USER;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("name", cursor.getString(0));
user.put("email", cursor.getString(1));
user.put("user_id", cursor.getInt(2));
user.put("api_key", cursor.getString(3));
}
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
return user;
}
public HashMap<String, Integer> getUserAuthorizations() throws Exception{
HashMap<String, Integer> auth = new HashMap<String, Integer>();
String selectQuery = "SELECT * FROM " + TABLE_AUTH;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
auth.put("pro_app", cursor.getInt(0));
}
cursor.close();
db.close();
// return user
try {
int authINT = auth.get("pro_app");
} catch (Exception e){
Crashlytics.log("this is what is throwing the error");
Crashlytics.logException(e);
}
int authINTNEXT = auth.get("pro_app");
return auth;
}
或者我有另一种根据列名获取记录的方法,如下代码所示
public static String getString(Cursor cursor, String fieldName, String nullValue) {
int column = cursor.getColumnIndex(fieldName);
if (cursor.isNull(column))
return nullValue;
else
return cursor.getString(column);
}
public static int getInt(Cursor cursor, String fieldName, int nullValue) {
int column = cursor.getColumnIndex(fieldName);
if (cursor.isNull(column))
return nullValue;
else
return cursor.getInt(column);
}
像这样的工具
public HashMap<String, Object> getUserDetails() {
HashMap<String, Object> user = new HashMap<String, Object>();
String selectQuery = "SELECT * FROM " + TABLE_USER;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("name", getString(cursor , NAME_COLUMN, ""));
user.put("email", getString(cursor , EMAIL_COLUMN, ""));
user.put("user_id", getInt(cursor ,USER_ID_COLUMN,0));
user.put("api_key",getString(cursor , API_KEY_COLUMN, ""));
}
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
return user;
}
做其他方法