你好我有问题请看我的代码:
public void showfal() {
Cursor cu = database.rawQuery("SELECT * FROM fal WHERE startdate = 'G.todaydate'", null);
while (cu.moveToNext()) {
farvardin.add(cu.getString(cu.getColumnIndex("farvardin")) + "");
ordibehesht.add(cu.getString(cu.getColumnIndex("ordibehesht")) + "");
khordad.add(cu.getString(cu.getColumnIndex("khordad")) + "");
tir.add(cu.getString(cu.getColumnIndex("tir")) + "");
mordad.add(cu.getString(cu.getColumnIndex("mordad")) + "");
shahrivar.add(cu.getString(cu.getColumnIndex("shahrivar")) + "");
mehr.add(cu.getString(cu.getColumnIndex("mehr")) + "");
aban.add(cu.getString(cu.getColumnIndex("aban")) + "");
azar.add(cu.getString(cu.getColumnIndex("azar")) + "");
dey.add(cu.getString(cu.getColumnIndex("dey")) + "");
bahman.add(cu.getString(cu.getColumnIndex("bahman")) + "");
esfand.add(cu.getString(cu.getColumnIndex("esfand")) + "");
}
cu.close();
}
当我调用showfal()时,我收到::
08-07 11:52:00.545: E/AndroidRuntime(6362): FATAL EXCEPTION: main
08-07 11:52:00.545: E/AndroidRuntime(6362): java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.aradmobaile.mhd.fal/ir.aradmobaile.mhd.fal.Activity_dailyfal}: java.lang.NullPointerException
08-07 11:52:00.545: E/AndroidRuntime(6362): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-07 11:52:00.545: E/AndroidRuntime(6362): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-07 11:52:00.545: E/AndroidRuntime(6362): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-07 11:52:00.545: E/AndroidRuntime(6362): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-07 11:52:00.545: E/AndroidRuntime(6362): at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 11:52:00.545: E/AndroidRuntime(6362): at android.os.Looper.loop(Looper.java:137)
08-07 11:52:00.545: E/AndroidRuntime(6362): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-07 11:52:00.545: E/AndroidRuntime(6362): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 11:52:00.545: E/AndroidRuntime(6362): at java.lang.reflect.Method.invoke(Method.java:511)
08-07 11:52:00.545: E/AndroidRuntime(6362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-07 11:52:00.545: E/AndroidRuntime(6362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-07 11:52:00.545: E/AndroidRuntime(6362): at dalvik.system.NativeStart.main(Native Method)
08-07 11:52:00.545: E/AndroidRuntime(6362): Caused by: java.lang.NullPointerException
08-07 11:52:00.545: E/AndroidRuntime(6362): at ir.aradmobaile.mhd.fal.Activity_dailyfal.showfal(Activity_dailyfal.java:167)
08-07 11:52:00.545: E/AndroidRuntime(6362): at ir.aradmobaile.mhd.fal.Activity_dailyfal.onCreate(Activity_dailyfal.java:39)
08-07 11:52:00.545: E/AndroidRuntime(6362): at android.app.Activity.performCreate(Activity.java:5104)
08-07 11:52:00.545: E/AndroidRuntime(6362): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-07 11:52:00.545: E/AndroidRuntime(6362): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-07 11:52:00.545: E/AndroidRuntime(6362): ... 11 more
08-07 11:57:00.549: E/InputDispatcher(368): channel '534453b4 ir.aradmobaile.mhd.fal/ir.aradmobaile.mhd.fal.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
08-07 11:57:00.549: E/InputDispatcher(368): channel '5355de34 Toast (server)' ~ Channel is unrecoverably broken and will be disposed!
public class Activity_dailyfal extends MainActivity {
public static Database db;
public SQLiteDatabase database;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_fourth);
db = new Database(this);
db.database();
db.open();
showfal();
}
这是我的数据库活动:
package ir.aradmobaile.mhd.fal;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Database扩展了SQLiteOpenHelper {
// public final String path = "data/data/ir.aradmobaile.dabiri.fal/databases/"; // adresi ke data barname mire dakhelesh
public final String Name = "arad_db"; // name of database
public SQLiteDatabase mydb;
private final Context mycontext;
公共数据库(上下文上下文){
super(context, "arad_db", null, 1);//set name of data base
mycontext = context;
}
@Override
public void onCreate(SQLiteDatabase arg0) {
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public void database() {
boolean checkdb = checkdb();
if (checkdb) {
// means database exist
} else {
// means database not exist
this.getReadableDatabase();
try {
copydatabase();
}
catch (IOException e) {
}
}
}
public void open() {
mydb = SQLiteDatabase.openDatabase(G.DIR_DATABASE + Name, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public void close() {
mydb.close();
}
public boolean checkdb() {
SQLiteDatabase db = null;
try {
db = SQLiteDatabase.openDatabase(G.DIR_DATABASE + Name, null, SQLiteDatabase.OPEN_READONLY);
}
catch (SQLException e)
{
}
return db != null ? true : false;
}
// baraye copy kardan az dakhel barname(assets) be database
public void copydatabase() throws IOException {
OutputStream myOutput = new FileOutputStream(G.DIR_DATABASE + Name);
byte[] buffer = new byte[1024];
int length;
InputStream myInput = mycontext.getAssets().open("arad_db");
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}
public String Display(int row, int fild) {
Cursor cu = mydb.query("push", null, null, null, null, null, null);
cu.moveToPosition(row);
String name = cu.getString(fild);
return name;
}
public String Display2(int row, int fild) {
Cursor cu = mydb.query("json_length", null, null, null, null, null, null);
cu.moveToPosition(row);
String name = cu.getString(fild);
cu.close();
return name;
}
public String Display3(int row, int fild) {
Cursor cu = mydb.query("fal", null, null, null, null, null, null);
cu.moveToPosition(row);
String name = cu.getString(fild);
cu.close();
return name;
}
public String Display4(int row, int fild) {
Cursor cu = mydb.query("json_length2", null, null, null, null, null, null);
cu.moveToPosition(row);
String name = cu.getString(fild);
cu.close();
return name;
}
public void inser(int id, String name, String user, String pass, String jj) {
ContentValues cv = new ContentValues();
cv.put("push_id", id);
cv.put("push_title", name);
cv.put("push_subject", user);
cv.put("push_body", pass);
cv.put("push_url", jj);
mydb.insert("push", "push_id", cv);
}
public void inser2(int id, int id2) {
ContentValues cv = new ContentValues();
cv.put("id_num", id);
cv.put("json_length", id2);
mydb.insert("json_length", "id_num", cv);
}
public void inser3(int id, String far, String ord, String kho, String tir, String mor, String sha, String meh, String aba, String aza, String dey, String bah, String esf, String date) {
ContentValues cv = new ContentValues();
cv.put("id", id);
cv.put("farvardin", far);
cv.put("ordibehesht", ord);
cv.put("khordad", kho);
cv.put("tir", tir);
cv.put("mordad", mor);
cv.put("shahrivar", sha);
cv.put("mehr", meh);
cv.put("aban", aba);
cv.put("azar", aza);
cv.put("dey", dey);
cv.put("bahman", bah);
cv.put("esfand", esf);
cv.put("startdate", date);
mydb.insert("fal", "id", cv);
}
我检查了我的桌子,这里有2条记录。 我困惑哪里是我的错?
答案 0 :(得分:-1)
我认为你的查询不对。
在访问之前检查游标是否为null。
喜欢:while (cu != null && cu.moveToNext()) {
答案 1 :(得分:-1)
而是使用cloumn的名称是否正确。
Cursor cu = database.rawQuery("SELECT * FROM fal WHERE startdate='"+ G.todaydate+"';", null);
是的,在访问之前检查是否(cursor!= null)。