0

我正在编码 Android,sql atm,我需要你的帮助。无法启动应用程序,因为我有一个空指针,我找不到它...

所以我试着读了很多书,但我认为我并没有清楚地理解这里的一切。所以如果你能试着告诉我哪里出了问题,那就太棒了。

你能帮我吗?

感谢所有阅读本文的人!:)

>public class DBAdapter
>{
>   //Variables and so on..
>   public static final String KEY_ROWID = "_id";
>   public static final String KEY_ISBN = "isbn";
>   public static final String KEY_TITLE = "title";
>   public static final String KEY_PUBLISHER ="publisher";
>   private static final String TAG ="DBAdapter";
>   private static final String DATABASE_NAME = "books";
>   private static final String DATABASE_TABLE = "titles";
>   private static final int DATABASE_VERSION = 1;
>   private static final String DATABASE_CREATE = "create table titles (_id integer >primary key autoincrement, "+ "isbn text not null, title text not null, "+ "publisher >text not null);";
>   
>   private final Context context;
>   
>   private DatabaseHelper DBHelper;
>   private SQLiteDatabase db;
>   
>   public DBAdapter(Context ctx)
>   {
>       this.context = ctx;
>       DBHelper = new DatabaseHelper(context);
>   }
>   
>   private static class DatabaseHelper extends SQLiteOpenHelper
>   {
>       DatabaseHelper(Context context)
>       {
>           super(context, DATABASE_NAME, null, DATABASE_VERSION);
>       }
>       @Override
>       public void onCreate(SQLiteDatabase db)
>       {
>           db.execSQL(DATABASE_CREATE);
>       }
>       @Override
>       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
>       {
>           Log.w(TAG, "Upgrading database from version "+oldVersion+" to >"+newVersion+" which will destory all old data");
>           db.execSQL("DROP TABLE IF EXISTS titles");
>           onCreate(db);
>       }
>   }
>
>   //opens database
>   public DBAdapter open() throws SQLException
>   {
>       db = DBHelper.getWritableDatabase();
>       return this;
>   }
>   
>   //closes database
>   public void close()
>   {
>       DBHelper.close();
>   }
>   
>   //insert title to database
>   ContentValues initialValues = new ContentValues();
>   public long insertTitle(String isbn, String title, String publisher)
>   {
>       initialValues.put(KEY_ISBN, isbn);
>       initialValues.put(KEY_TITLE, title);
>       initialValues.put(KEY_PUBLISHER, publisher);
>       return db.insert(DATABASE_TABLE, null, initialValues);
>   }
>   
>   //Delete specific title
>   public boolean deleteTitle(long rowId)
>   {
>       return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
>   }
>   
>   //Retrieves all titles
>   public Cursor getAllTitles()
>   {
>       return db.query(DATABASE_TABLE, new String[]
>       {KEY_ROWID, KEY_ISBN, KEY_TITLE, KEY_PUBLISHER}, null, null, null, null, >null);
>   }
>   
>   //Retrieves a particular title
>   public Cursor getTitle(Long rowId) throws SQLException
>   {
>       Cursor mCursor = db.query(true, DATABASE_TABLE, new String[]
>               {KEY_ROWID, KEY_ISBN, KEY_TITLE, KEY_PUBLISHER}, KEY_ROWID >+ "=" + rowId, null, null, null, null, null);
>       if(mCursor != null)
>       {
>           mCursor.moveToFirst();
>       }
>       return mCursor;
>   }
>   
>   //Update a title
>   ContentValues args = new ContentValues();
>   public boolean updateTitle(long rowId, String title, String isbn, String publisher)
>   {
>       args.put(KEY_ISBN, isbn);
>       args.put(KEY_TITLE, title);
>       args.put(KEY_PUBLISHER, publisher);
>       return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
>   }
>}

编辑:

这是我使用 DBAdapter 类的代码:

public class DatabasActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DBAdapter db = new DBAdapter(this);

        //Add 2 titles
        db.open();
        long id;
        id = db.insertTitle("0470285818", "C# 2008 Programmer's Reference", "Wrox");
        id = db.insertTitle("047017661X", "Professional Windows Vista Gadgets Programming", "Wrox");
        db.close();

        //Get all titles
        db.open();
        Cursor c = db.getAllTitles();
        if(c.moveToFirst())
        {
            do
            {
                DisplayTitle(c);
            }
            while(c.moveToNext());
            {

            }
        }
        db.close();

        //get a title
        db.open();
        Cursor c1 = db.getTitle((long) 2);
        if(c1.moveToFirst())
        {
            DisplayTitle(c1);
        }
        else
        {
            Toast.makeText(this, "No title found", Toast.LENGTH_LONG).show();
        }
        db.close();

        //Update a title
        db.open();
        if(db.updateTitle(1, "0470285818", "C# 2008 Programmer's Reference", "Wrox Press"))
        {
            Toast.makeText(this, "Update successful", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(this, "Update failed", Toast.LENGTH_LONG).show();
        }
        db.close();
        //Retrive the updated title
        Cursor c2 = db.getTitle((long) 1);
        if(c.moveToFirst())
        {
            DisplayTitle(c2);
        }
        else
        {
            Toast.makeText(this, "No title found", Toast.LENGTH_LONG).show();
        }
    }

    public void DisplayTitle(Cursor c)
    {
        Toast.makeText(this, "id: "+c.getString(0)+"\n"+"ISBN: "+c.getString(1)+"\n"+"Title: "+c.getString(2)+"\n"+"Publisher: "+c.getString(3), Toast.LENGTH_LONG).show();
    }

}

我设法从我的 logcat 中获得了一些输出,这里是:

06-14 08:43:00.882: D/AndroidRuntime(426): Shutting down VM
06-14 08:43:00.892: W/dalvikvm(426): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-14 08:43:00.912: E/AndroidRuntime(426): FATAL EXCEPTION: main
06-14 08:43:00.912: E/AndroidRuntime(426): java.lang.RuntimeException: Unable to start activity ComponentInfo{databas.test.org/databas.test.org.DatabasActivity}: java.lang.NullPointerException
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.os.Looper.loop(Looper.java:123)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 08:43:00.912: E/AndroidRuntime(426):  at java.lang.reflect.Method.invokeNative(Native Method)
06-14 08:43:00.912: E/AndroidRuntime(426):  at java.lang.reflect.Method.invoke(Method.java:507)
06-14 08:43:00.912: E/AndroidRuntime(426):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 08:43:00.912: E/AndroidRuntime(426):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 08:43:00.912: E/AndroidRuntime(426):  at dalvik.system.NativeStart.main(Native Method)
06-14 08:43:00.912: E/AndroidRuntime(426): Caused by: java.lang.NullPointerException
06-14 08:43:00.912: E/AndroidRuntime(426):  at databas.test.org.DBAdapter.insertTitle(DBAdapter.java:74)
06-14 08:43:00.912: E/AndroidRuntime(426):  at databas.test.org.DatabasActivity.onCreate(DatabasActivity.java:24)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-14 08:43:00.912: E/AndroidRuntime(426):  ... 11 more
4

2 回答 2

2

我看到的唯一潜在的 NPE 是在没有打电话open()的时候DbAdapter

于 2012-06-14T08:05:47.860 回答
0

我认为在

create table titles (_id integer >primary key autoincrement, "+ "isbn text not null, title text not null, "+ "publisher >text not null)

">" 在那里无效......

于 2012-06-14T08:03:54.820 回答