-2

我的 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)

请检查程序并给我解决方案,它显示异常,数据库中有值,我正在检索它们..

4

1 回答 1

2
Cursor data = db.query("Mycon", fields,null, null, null, null, null);

你还没有初始化你的db对象。

看起来你已经有了初始化代码,即SQLiteOpenHelper子类等等DatabaseAdapter。调用getReadableDatabase()helper 来获取一个SQLiteDatabase可以运行的对象query()

于 2013-09-18T09:54:44.140 回答