-1

这个问题可能已经在某个地方解决了,但我无法正确解决,所以请帮助我刚开始使用数据库,所以我对此知之甚少。

我正在尝试从创建的数据库中检索数据,并且调用了一个空指针异常。

这是显示列表 ID 的类。

ReviewList.java

public class ReviewList extends ListActivity{
    private ReviewsDataSource datasource;
    // Place Details
    PlaceDetails placeDetails;

    @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.review);
        Button b = (Button) findViewById(R.id.reviewbutton);
        datasource = new ReviewsDataSource(this);
        datasource.open();

        List<Review> values = datasource.getAllReviews(placeDetails.result.reference);


        // Use the SimpleCursorAdapter to show the
        // elements in a ListView
        ArrayAdapter<Review> adapter = new ArrayAdapter<Review>(this,
        android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                @SuppressWarnings("unchecked")
                ArrayAdapter<Review> adapter = (ArrayAdapter<Review>) getListAdapter();
               Review review = null;

               EditText et = (EditText) findViewById(R.id.reviewtext);
               String text= et.getText().toString();

               review=datasource.createReview(text, placeDetails.result.reference);
               adapter.add(review);
               adapter.notifyDataSetChanged();
            }
        });
      }




     @Override
      protected void onResume() {
        datasource.open();
        super.onResume();
      }

      @Override
      protected void onPause() {
        datasource.close();
        super.onPause();
      }


}

这是复习课

public class Review {

    private String id;
      private String review;

      public String getId() {
        return id;
      }

      public void setId(String id) {
        this.id = id;
      }

      public String getReview() {
        return review;
      }

      public void setReview(String review) {
        this.review = review;
      }

      // Will be used by the ArrayAdapter in the ListView
      @Override
      public String toString() {
        return review;
      }
}

这是进行所有数据库操作的ReviewsDataSource.java。

public class ReviewsDataSource {

     private SQLiteDatabase database;
     private Database dbHelper;

     private String[] allColumns = { Database.COLUMN_ID,
              Database.COLUMN_REVIEW };


     public ReviewsDataSource(Context context) {
            dbHelper = new Database(context);
          }

     public void open() throws SQLException {
            database = dbHelper.getWritableDatabase();
          }

     public void close() {
            dbHelper.close();
          }
     public Review createReview(String review, String reference) {
            ContentValues values = new ContentValues();
            values.put(Database.COLUMN_REVIEW, review);
            values.put(Database.COLUMN_ID, reference);
            database.insert(Database.TABLE_REVIEWS, null, values);
            Cursor cursor = database.query(Database.TABLE_REVIEWS,
                allColumns, Database.COLUMN_ID + " = " + reference, null,
                null, null, null);
            cursor.moveToFirst();
            Review newReview = cursorToReview(cursor);
            cursor.close();
            return newReview;
          }

     public void deleteReview(Review comment) {
            String id = comment.getId();
            System.out.println("Comment deleted with id: " + id);
            database.delete(Database.TABLE_REVIEWS, Database.COLUMN_ID
                + " = " + id, null);
          }
     public List<Review> getAllReviews(String reference) {
            List<Review> comments = new ArrayList<Review>();
            Cursor cursor = database.query(Database.TABLE_REVIEWS,
                    allColumns, null, null, null, null, null);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
              Review review = cursorToReview(cursor);
              if(cursor.getString(0)==reference)
              {
              comments.add(review);
              }
              cursor.moveToNext();
            }
            // Make sure to close the cursor
            cursor.close();
            return comments;


     }




    private Review cursorToReview(Cursor cursor) {
        // TODO Auto-generated method stub
        Review review = new Review();
        review.setId(cursor.getString(0));
        review.setReview(cursor.getString(1));
        return review;
    }

}

最后是 database.java 类

public class Database extends SQLiteOpenHelper {

    public static final String TABLE_REVIEWS = "reviews";
      public static final String COLUMN_ID = "_id";
      public static final String COLUMN_REVIEW = "REVIEW";

      private static final String DATABASE_NAME = "reviews.db";
      private static final int DATABASE_VERSION = 1;

      private static final String DATABASE_CREATE = "create table "
              + TABLE_REVIEWS + "(" + COLUMN_ID
              + " text not null, " + COLUMN_REVIEW
              + " text not null);";

      public Database(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
          }

    @Override
    public void onCreate(SQLiteDatabase database) {
        // TODO Auto-generated method stub
        database.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        Log.w(Database.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_REVIEWS);
        onCreate(db);
    }
}

这是我的日志猫

 02-21 10:49:08.957: E/AndroidRuntime(24586): FATAL EXCEPTION: main
 02-21 10:49:08.957: E/AndroidRuntime(24586):
 java.lang.RuntimeException: Unable to start activity
 ComponentInfo{com.example.fueled_assignment_1/com.example.fueled_assignment_1.ReviewList}:
 java.lang.NullPointerException 02-21 10:49:08.957:
 E/AndroidRuntime(24586):   at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
 02-21 10:49:08.957: E/AndroidRuntime(24586):   at
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
 02-21 10:49:08.957: E/AndroidRuntime(24586):   at
 android.app.ActivityThread.access$1500(ActivityThread.java:135) 02-21
 10:49:08.957: E/AndroidRuntime(24586):     at
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
 02-21 10:49:08.957: E/AndroidRuntime(24586):   at
 android.os.Handler.dispatchMessage(Handler.java:99) 02-21
 10:49:08.957: E/AndroidRuntime(24586):     at
 android.os.Looper.loop(Looper.java:150) 02-21 10:49:08.957:
 E/AndroidRuntime(24586):   at
 android.app.ActivityThread.main(ActivityThread.java:4389) 02-21
 10:49:08.957: E/AndroidRuntime(24586):     at
 java.lang.reflect.Method.invokeNative(Native Method) 02-21
 10:49:08.957: E/AndroidRuntime(24586):     at
 java.lang.reflect.Method.invoke(Method.java:507) 02-21 10:49:08.957:
 E/AndroidRuntime(24586):   at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
 02-21 10:49:08.957: E/AndroidRuntime(24586):   at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 02-21
 10:49:08.957: E/AndroidRuntime(24586):     at
 dalvik.system.NativeStart.main(Native Method) 02-21 10:49:08.957:
 E/AndroidRuntime(24586): **Caused by: java.lang.NullPointerException
 02-21 10:49:08.957: E/AndroidRuntime(24586):   at
 com.example.fueled_assignment_1.ReviewList.onCreate(ReviewList.java:26)**
 02-21 10:49:08.957: E/AndroidRuntime(24586):   at
 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
 02-21 10:49:08.957: E/AndroidRuntime(24586):   at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
 02-21 10:49:08.957: E/AndroidRuntime(24586):   ... 11 more

请帮我解决我一团糟的问题

4

1 回答 1

0

您将不得不编写一个自定义数组适配器 - 使用 Review 作为您的 DAO

检查此示例 仅将文本作为数组

图像和数组

让我知道

于 2013-02-21T05:32:23.963 回答