0

这可能很简单,但会出错,所以请帮助我。我有列表视图,并且在每一行中都有按钮,当我单击按钮时,它应该采用该特定行的标题并将其插入数据库。当我单击按钮时,它实际上给了我标题,但是我在将该标题插入数据库时​​遇到问题。在 LazyAdapter 类中,我有以下代码

        Button play = (Button)vi.findViewById(R.id.imageArrow);
    play.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("play clicked!");
            String pgm_title = title.getText().toString();
            System.out.println("item selected: "+ pgm_title);

            CustomizedListView cust = new CustomizedListView();
            cust.InsertPlayItem(pgm_title);
        }
    });

在主要活动中我有这个代码。这段代码有什么问题?

     public void InsertPlayItem(String pgm_title) {
    // TODO Auto-generated method stub
    DatabaseHandler db = new DatabaseHandler(getBaseContext());//getting error here
    db.insertPlayList(pgm_title);
}

出现以下错误:

12-26 16:03:00.751: W/dalvikvm(5861): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-26 16:03:00.772: E/AndroidRuntime(5861): FATAL EXCEPTION: main
12-26 16:03:00.772: E/AndroidRuntime(5861): java.lang.NullPointerException
12-26 16:03:00.772: E/AndroidRuntime(5861):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
12-26 16:03:00.772: E/AndroidRuntime(5861):     at com.example.devices.DatabaseHandler.insertPlayList(DatabaseHandler.java:422)
12-26 16:03:00.772: E/AndroidRuntime(5861):     at com.example.programs.CustomizedListView.InsertPlayItem(CustomizedListView.java:137)
12-26 16:03:00.772: E/AndroidRuntime(5861):     at com.example.programs.LazyAdapter$1.onClick(LazyAdapter.java:131)
12-26 16:03:00.772: E/AndroidRuntime(5861):     at android.view.View.performClick(View.java:2485)
12-26 16:03:00.772: E/AndroidRuntime(5861):     at android.view.View$PerformClick.run(View.java:9080)
12-26 16:03:00.772: E/AndroidRuntime(5861):     at android.os.Handler.handleCallback(Handler.java:587)
12-26 16:03:00.772: E/AndroidRuntime(5861):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-26 16:03:00.772: E/AndroidRuntime(5861):     at android.os.Looper.loop(Looper.java:123)
12-26 16:03:00.772: E/AndroidRuntime(5861):     at android.app.ActivityThread.main(ActivityThread.java:3683)
12-26 16:03:00.772: E/AndroidRuntime(5861):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 16:03:00.772: E/AndroidRuntime(5861):     at java.lang.reflect.Method.invoke(Method.java:507)
12-26 16:03:00.772: E/AndroidRuntime(5861):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-26 16:03:00.772: E/AndroidRuntime(5861):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-26 16:03:00.772: E/AndroidRuntime(5861):     at dalvik.system.NativeStart.main(Native Method)

还有我的数据库部分:

            public void insertPlayList(String playname) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(play_list, playname);
        // Inserting Row
        db.insert(Personalize, null, values);
        db.close(); // Closing database connection
    }
4

1 回答 1

1

最后我得到了..在 LazyAdapter 类中只有我喜欢这个。

       Button play = (Button)vi.findViewById(R.id.imageArrow);
    play.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("play clicked!");
            String pgm_title = title.getText().toString();
            System.out.println("item selected: "+ pgm_title);
            DatabaseHandler db = new DatabaseHandler(activity);
            db.insertPlayList(pgm_title);
                        }
         }
于 2012-12-26T11:15:40.890 回答