我正在学习android。我正在尝试创建一个从EditText中的用户获取数据的应用程序。将这些数据与SQLite数据库进行比较,并在另一个EditText中返回数据。
运行我的代码应用程序后关闭ieForce Close。即不幸的是你的申请已被关闭。 请帮我比较和检索sqlite数据库中的数据并在EditText中显示它。
My Code is:
DatabaseHelper Class:
public Cursor getData() {
SQLiteDatabase db = this.getWritableDatabase();
String english = et1.getText().toString();
//查询以检查数据库中的数据
Cursor cursor = db.rawQuery("select MSICTm_AutoTarget_Word from" + TABLE_NAME + "where MDICTm_Source_Word="+english, null);
et2.setText("");
if (cursor!=null) {
cursor.moveToFirst();
do {
String MSICTm_AutoTarget_Word = cursor.getString(cursor.getColumnIndex("MSICTm_AutoTarget_Word"));
et2.append("MSICTm_AutoTarget_Word"+cursor.getString(2)+cursor);
} while (cursor.moveToNext());
}
return cursor;
}
主要活动类:
public void viewData() {
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Cursor cursor = mydb.getData();
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String MSICTm_AutoTarget_Word = cursor.getString(cursor.getColumnIndex("MSICTm_AutoTarget_Word"));
et2.append("MSICTm_AutoTarget_Word" + MSICTm_AutoTarget_Word + cursor);
}
while (cursor.moveToNext());
}
}
}
});
}
// logcat的
04-27 14:10:34.698 2420-2420/com.example.mcsipl_hplaptop240_1.language I/art: Not late-enabling -Xcheck:jni (already on)
04-27 14:10:34.826 2420-2420/com.example.mcsipl_hplaptop240_1.language W/System: ClassLoader referenced unknown path: /data/app/com.example.mcsipl_hplaptop240_1.language-1/lib/x86
04-27 14:10:44.248 2420-2420/com.example.mcsipl_hplaptop240_1.language W/System: ClassLoader referenced unknown path: /data/app/com.example.mcsipl_hplaptop240_1.language-1/lib/x86
04-27 14:10:45.466 2420-2420/com.example.mcsipl_hplaptop240_1.language W/art: Verification of android.support.v7.view.ActionMode android.support.v7.app.AppCompatDelegateImplV7.startSupportActionModeFromWindow(android.support.v7.view.ActionMode$Callback) took 369.473ms
04-27 14:10:45.677 2420-2426/com.example.mcsipl_hplaptop240_1.language W/art: Suspending all threads took: 104.833ms
04-27 14:10:46.390 2420-2430/com.example.mcsipl_hplaptop240_1.language W/art: Suspending all threads took: 525.430ms
04-27 14:10:46.398 2420-2430/com.example.mcsipl_hplaptop240_1.language I/art: Background sticky concurrent mark sweep GC freed 15945(1793KB) AllocSpace objects, 8(176KB) LOS objects, 66% free, 1257KB/3MB, paused 526.247ms total 537.371ms
04-27 14:10:46.713 2420-2426/com.example.mcsipl_hplaptop240_1.language W/art: Suspending all threads took: 137.656ms
04-27 14:10:47.051 2420-2420/com.example.mcsipl_hplaptop240_1.language W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-27 14:10:47.300 2420-2426/com.example.mcsipl_hplaptop240_1.language W/art: Suspending all threads took: 223.122ms
04-27 14:10:48.227 2420-2426/com.example.mcsipl_hplaptop240_1.language W/art: Suspending all threads took: 149.071ms
04-27 14:10:48.748 2420-2426/com.example.mcsipl_hplaptop240_1.language W/art: Suspending all threads took: 166.289ms
04-27 14:10:48.913 2420-2420/com.example.mcsipl_hplaptop240_1.language W/FileUtils: Failed to chmod(/data/user/0/com.example.mcsipl_hplaptop240_1.language/databases/LNGTRNS.db): android.system.ErrnoException: chmod failed: EPERM (Operation not permitted)
04-27 14:10:48.914 2420-2420/com.example.mcsipl_hplaptop240_1.language D/AndroidRuntime: Shutting down VM
--------- beginning of crash
04-27 14:10:48.914 2420-2420/com.example.mcsipl_hplaptop240_1.language E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mcsipl_hplaptop240_1.language, PID: 2420
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mcsipl_hplaptop240_1.language/com.example.mcsipl_hplaptop240_1.language.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 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 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.mcsipl_hplaptop240_1.language.MainActivity.viewData(MainActivity.java:87)
at com.example.mcsipl_hplaptop240_1.language.MainActivity.onCreate(MainActivity.java:34)
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)
答案 0 :(得分:0)
您是否在主要活动中初始化了dbhelper类?
dbclass mydb=new dbclass();
答案 1 :(得分:0)
您的错误似乎是
引起:java.lang.NullPointerException:尝试调用虚方法' void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'在空对象引用上
在viewData()方法中设置onClickListener时,按钮b1未初始化。
初始化按钮b1
b1 = (Button) findViewbyId(R.id.your_button_id);
在设置点击监听器之前