我的XML文件是
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
在这些数据库中,我从数据库中获取值并在列表视图中显示它们。 我的主要活动是 包com.example.kern;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Second extends ListActivity {
DatabaseAdapter helper;
SQLiteDatabase db;
private CursorAdapter dataSource;
private static final String fields[] = { "Name", "Numb"};
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
helper=new DatabaseAdapter(this);
helper.open();
lv=(ListView)findViewById(android.R.id.list);
Cursor data = db.query("Mycon", fields,null, null, null, null, null);
dataSource = new SimpleCursorAdapter(this,
R.layout.activity_second, data, fields,
new int[] { R.id.name, R.id.number });
lv.setAdapter(dataSource);
}
}
和mydatabase适配器类是 包com.example.kern;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.example.kern.DatabaseAdapter;
import com.example.kern.DatabaseAdapter.DatabaseHelper;
public class DatabaseAdapter {
DatabaseHelper helper;
SQLiteDatabase db;
static final int VERSION=2;
static final String DATA_NAME="Mycontsc";
private static final String query = "create table Mycon(id Integer primary
key,Name text,Numb text)"; //the query has been changed
private final Context mCtx;
public DatabaseAdapter(Context ctx) {
this.mCtx = ctx;
}
//DatabaseHelper has become a subclass
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATA_NAME, null, VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
arg0.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public DatabaseAdapter open() throws SQLException {
helper = new DatabaseHelper(mCtx);
db=helper.getWritableDatabase();
db=helper.getReadableDatabase();
return this;
}
public void close() {
helper.close();
}
public long AddDetail(String name,String num)
{
ContentValues vals=new ContentValues();
vals.put("Name",name);
vals.put("Numb", num);
long a=db.insert("Mycon", null, vals);
return a;
}
}
它在logcat中显示空指针异常
09-18 10:32:02.631: E/AndroidRuntime(2286): FATAL EXCEPTION: main
09-18 10:32:02.631: E/AndroidRuntime(2286): java.lang.RuntimeException: Unable to
start activity ComponentInfo{com.example.kern/com.example.kern.Second}:
java.lang.NullPointerException
09-18 10:32:02.631: E/AndroidRuntime(2286): Caused by: java.lang.NullPointerException
09-18 10:32:02.631: E/AndroidRuntime(2286): at
com.example.kern.Second.onCreate(Second.java:29)
请检查程序并给我解决方案,它显示异常,alerady数据库中有值,我正在重新启动它们。
答案 0 :(得分:2)
Cursor data = db.query("Mycon", fields,null, null, null, null, null);
您尚未初始化db
对象。
看起来您已经拥有SQLiteOpenHelper
中的初始化代码,即DatabaseAdapter
子类等。在帮助程序上调用getReadableDatabase()
以获取可以SQLiteDatabase
运行的query()
对象。