如何在 android 中检查数据库中的项目
How to check items from database in android
我想从 server 加载数据到我的应用程序中,当用户单击按钮时从 server[=59 保存此 post(内容) =] 变成 SQLiteDatabse
.
我想去 activity
, fist 检查 database
, if exists 这个 post 到 database
更改按钮颜色。
对于这份工作,我检查 posts 与 Title!
意思是如果这个post标题变成databse
setBtnColor="R.color.primaryColor"
,另一边如果这个post标题 不属于 database
setBtnCOlor="R.color.black"
。
我写了下面的代码,但是当运行应用程序时,显示强制关闭错误。
DatabaseHelperClass代码:
public boolean checkFavPost(String title) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. set cursor for read row
Cursor cursor = db.rawQuery("SELECT * FROM " + FavContract.favInfo.TABLE_NAME + " WHERE " +
FavContract.favInfo.FAV_TBL_PTitle + " = ?", new String[]{title});
if (cursor.getCount() > 0) {
return true;
} else {
return false;
}
}
Activity 代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post_show_page);
bindActivity();
// Initialize
postShow_favPost = (ShineButton) mToolbar.findViewById(R.id.post_FavImage);
post_cover = (ImageView) findViewById(R.id.postShow_cover_image);
postShow_title = (TextView) findViewById(R.id.postShow_title);
postShow_title2 = (TextView) findViewById(R.id.postShow_titleBig);
//postShow_content = (TextView) findViewById(R.id.postShow_content_text);
postShow_dateTime = (TextView) findViewById(R.id.postShow_man_date_text);
postShow_author = (TextView) findViewById(R.id.postShow_man_author_text);
postShow_category = (TextView) findViewById(R.id.postShow_man_category_text);
title_sliding = (TextView) findViewById(R.id.post_sliding_title);
comment_Recyclerview = (RecyclerView) findViewById(R.id.comment_recyclerView);
post_content_web = (WebView) findViewById(R.id.postShow_content_web);
//Give Data
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
title = bundle.getString("title");
image = bundle.getString("image");
content = bundle.getString("content");
dateTime = bundle.getString("dateTime");
author = bundle.getString("author");
category = bundle.getString("category");
categoryID = bundle.getString("categoryID");
}
if (favDB.checkFavPost(title) == true) {
postShow_favPost.setBtnColor(R.color.colorPrimary);
} else {
postShow_favPost.setBtnColor(R.color.black);
}
显示此行的错误:
if (favDB.checkFavPost(title) == true) {
postShow_favPost.setBtnColor(R.color.colorPrimary);
} else {
postShow_favPost.setBtnColor(R.color.black);
}
LogCat 错误:
09-28 10:38:02.916 2033-2033/com.tellfa.colony E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tellfa.colony, PID: 2033
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tellfa.colony/com.tellfa.colony.Activities.PostShow_page}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.tellfa.colony.DBhelper.FavHelper.checkFavPost(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
at android.app.ActivityThread.access0(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.tellfa.colony.DBhelper.FavHelper.checkFavPost(java.lang.String)' on a null object reference
at com.tellfa.colony.Activities.PostShow_page.onCreate(PostShow_page.java:122)
at android.app.Activity.performCreate(Activity.java:6020)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2284)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
at android.app.ActivityThread.access0(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
亲爱的朋友,我知道这个错误显示NullPointerException
,但我是业余爱好者,我的问题是我该如何解决它?!
请不要给我负分,请帮助我。谢谢大家 <3
问题是你检查 null 的可能值
尝试用
之类的东西检查一下
if(value != null){
//and ur code
}
还学习了一些关于 nullpointexception 的知识
What is a NullPointerException, and how do I fix it?
您似乎还没有初始化 Class。
请使用构造函数参数初始化 DatabaseHelperClass。
DatabaseHelperClass favDB = new DatabaseHelperClass(this);
我想从 server 加载数据到我的应用程序中,当用户单击按钮时从 server[=59 保存此 post(内容) =] 变成 SQLiteDatabse
.
我想去 activity
, fist 检查 database
, if exists 这个 post 到 database
更改按钮颜色。
对于这份工作,我检查 posts 与 Title!
意思是如果这个post标题变成databse
setBtnColor="R.color.primaryColor"
,另一边如果这个post标题 不属于 database
setBtnCOlor="R.color.black"
。
我写了下面的代码,但是当运行应用程序时,显示强制关闭错误。
DatabaseHelperClass代码:
public boolean checkFavPost(String title) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. set cursor for read row
Cursor cursor = db.rawQuery("SELECT * FROM " + FavContract.favInfo.TABLE_NAME + " WHERE " +
FavContract.favInfo.FAV_TBL_PTitle + " = ?", new String[]{title});
if (cursor.getCount() > 0) {
return true;
} else {
return false;
}
}
Activity 代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post_show_page);
bindActivity();
// Initialize
postShow_favPost = (ShineButton) mToolbar.findViewById(R.id.post_FavImage);
post_cover = (ImageView) findViewById(R.id.postShow_cover_image);
postShow_title = (TextView) findViewById(R.id.postShow_title);
postShow_title2 = (TextView) findViewById(R.id.postShow_titleBig);
//postShow_content = (TextView) findViewById(R.id.postShow_content_text);
postShow_dateTime = (TextView) findViewById(R.id.postShow_man_date_text);
postShow_author = (TextView) findViewById(R.id.postShow_man_author_text);
postShow_category = (TextView) findViewById(R.id.postShow_man_category_text);
title_sliding = (TextView) findViewById(R.id.post_sliding_title);
comment_Recyclerview = (RecyclerView) findViewById(R.id.comment_recyclerView);
post_content_web = (WebView) findViewById(R.id.postShow_content_web);
//Give Data
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
title = bundle.getString("title");
image = bundle.getString("image");
content = bundle.getString("content");
dateTime = bundle.getString("dateTime");
author = bundle.getString("author");
category = bundle.getString("category");
categoryID = bundle.getString("categoryID");
}
if (favDB.checkFavPost(title) == true) {
postShow_favPost.setBtnColor(R.color.colorPrimary);
} else {
postShow_favPost.setBtnColor(R.color.black);
}
显示此行的错误:
if (favDB.checkFavPost(title) == true) {
postShow_favPost.setBtnColor(R.color.colorPrimary);
} else {
postShow_favPost.setBtnColor(R.color.black);
}
LogCat 错误:
09-28 10:38:02.916 2033-2033/com.tellfa.colony E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tellfa.colony, PID: 2033
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tellfa.colony/com.tellfa.colony.Activities.PostShow_page}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.tellfa.colony.DBhelper.FavHelper.checkFavPost(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
at android.app.ActivityThread.access0(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.tellfa.colony.DBhelper.FavHelper.checkFavPost(java.lang.String)' on a null object reference
at com.tellfa.colony.Activities.PostShow_page.onCreate(PostShow_page.java:122)
at android.app.Activity.performCreate(Activity.java:6020)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2284)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
at android.app.ActivityThread.access0(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
亲爱的朋友,我知道这个错误显示NullPointerException
,但我是业余爱好者,我的问题是我该如何解决它?!
请不要给我负分,请帮助我。谢谢大家 <3
问题是你检查 null 的可能值
尝试用
之类的东西检查一下if(value != null){
//and ur code
}
还学习了一些关于 nullpointexception 的知识 What is a NullPointerException, and how do I fix it?
您似乎还没有初始化 Class。
请使用构造函数参数初始化 DatabaseHelperClass。
DatabaseHelperClass favDB = new DatabaseHelperClass(this);