public class Rating {
private String _id;
private String _rating;
public Rating(){
}
public Rating(String id, String ration){
this._id = id;
this._rating = ration;
}
public Rating(String ration){
this._rating= ration;
}
public String getID(){
return _id;
}
public void setID(String id){
this._id = id;
}
public String getRating(){
return _rating;
}
public void setRating(String rating){
this._rating= rating;
}
@Override
public String toString(){
return _rating;
}
}
///////////////////////////////////////////////////// /
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME ="ratings.db";
public static final String DATABASE_TABLE ="ratings";
public static final String COLUMN_ID = "id";
public static final String COLUMN_RATING = "rating";
private static final String DATABASE_CREATE = "create table "
+ DATABASE_TABLE + "(" +
COLUMN_ID + " integer primary key autoincrement, " +
COLUMN_RATING + " text not null);";
public DatabaseHandler(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(DatabaseHandler.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
///////////////////////////////////////////////////// /
public class RatingDataSource {
private SQLiteDatabase database;
private DatabaseHandler dbHelper;
private String[] allColumns = { DatabaseHandler.COLUMN_ID,
DatabaseHandler.COLUMN_RATING };
public RatingDataSource(Context context) {
dbHelper = new DatabaseHandler(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public void addRating(Rating rating) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(DatabaseHandler.COLUMN_ID,rating.getID());
values.put(DatabaseHandler.COLUMN_RATING, rating.getRating());
database.insert(dbHelper.DATABASE_TABLE,null,values);
db.insert(dbHelper.DATABASE_TABLE,null,values);
db.close();
}
public Rating getRating(String id)
{
SQLiteDatabase db=dbHelper.getWritableDatabase();
Cursor cursor=db.query(dbHelper.DATABASE_TABLE,new String[]{dbHelper.COLUMN_ID,dbHelper.COLUMN_RATING},dbHelper.COLUMN_ID +"=?" ,new String[]{String.valueOf(id)},null,null,null,null);
if (cursor!=null)
cursor.moveToFirst();
Rating rating=new Rating((cursor.getString(0)),cursor.getString(1));
return rating;
}
public void deleteRating(Rating rating) {
SQLiteDatabase db=dbHelper.getReadableDatabase();
db.delete(dbHelper.DATABASE_TABLE,dbHelper.COLUMN_ID+"?",new String[]{String.valueOf(rating.getID())});
db.close();
}
public List<Rating> getAllRating()
{
List<Rating> RatingList=new ArrayList<Rating>();
Cursor cursor = database.query(DatabaseHandler.DATABASE_TABLE, allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Rating rating = cursorToRating(cursor);
RatingList.add(rating);
cursor.moveToNext();
}
cursor.close();
return RatingList;
}
private Rating cursorToRating(Cursor cursor){
Rating rating = new Rating();
rating.setID(cursor.getString(0));
rating.setRating(cursor.getString(1));
return rating;
}
}
//////////////这是我的错误
03-04 10:59:47.473: INFO/ActivityManager(277): START u0 {cmp=com.example.sampleebook/.GhasidesingelItem (has extras)} from pid 4030
03-04 10:59:47.483: ERROR/libEGL(36): called unimplemented OpenGL ES API
03-04 10:59:47.483: ERROR/libEGL(36): called unimplemented OpenGL ES API
03-04 10:59:47.483: ERROR/libEGL(36): called unimplemented OpenGL ES API
03-04 10:59:47.483: ERROR/libEGL(36): called unimplemented OpenGL ES API
03-04 10:59:47.483: ERROR/libEGL(36): called unimplemented OpenGL ES API
03-04 10:59:47.483: ERROR/libEGL(36): called unimplemented OpenGL ES API
03-04 10:59:47.483: ERROR/libEGL(36): called unimplemented OpenGL ES API
03-04 10:59:47.483: ERROR/libEGL(36): called unimplemented OpenGL ES API
03-04 10:59:47.483: ERROR/libEGL(36): called unimplemented OpenGL ES API
03-04 10:59:47.483: ERROR/libEGL(36): called unimplemented OpenGL ES API
03-04 10:59:47.483: WARN/WindowManager(277): Failure taking screenshot for (246x410) to layer 21020
03-04 10:59:47.934: DEBUG/dalvikvm(4030): GC_CONCURRENT freed 270K, 7% free 5878K/6312K, paused 75ms+27ms, total 192ms
03-04 10:59:48.113: DEBUG/AndroidRuntime(4030): Shutting down VM
03-04 10:59:48.113: WARN/dalvikvm(4030): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-04 10:59:48.173: ERROR/AndroidRuntime(4030): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sampleebook/com.example.sampleebook.GhasidesingelItem}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.sampleebook.RatingDataSource.getRating(RatingDataSource.java:46)
at com.example.sampleebook.GhasidesingelItem.onCreate(GhasidesingelItem.java:81)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
03-04 10:59:48.263: WARN/ActivityManager(277): Force finishing activity com.example.sampleebook/.GhasidesingelItem
03-04 10:59:48.273: WARN/ActivityManager(277): Force finishing activity com.example.sampleebook/.Ghasideh
03-04 10:59:48.643: INFO/Choreographer(277): Skipped 49 frames! The application may be doing too much work on its main thread.
03-04 10:59:48.643: ERROR/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
03-04 10:59:48.783: WARN/ActivityManager(277): Activity pause timeout for ActivityRecord{410dc930 u0 com.example.sampleebook/.GhasidesingelItem}
03-04 10:59:57.523: WARN/ActivityManager(277): Launch timeout has expired, giving up wake lock!
03-04 10:59:57.523: WARN/ActivityManager(277): Activity idle timeout for ActivityRecord{410dc930 u0 com.example.sampleebook/.GhasidesingelItem}
03-04 10:59:58.857: WARN/ActivityManager(277): Activity idle timeout for ActivityRecord{40f7c650 u0 com.example.sampleebook/.MyActivity}
03-04 11:00:07.615: WARN/ActivityManager(277): Activity destroy timeout for ActivityRecord{4108c680 u0 com.example.sampleebook/.Ghasideh}
03-04 11:00:07.615: WARN/ActivityManager(277): Activity destroy timeout for ActivityRecord{410dc930 u0 com.example.sampleebook/.GhasidesingelItem}
03-04 11:00:23.171: DEBUG/ExchangeService(670): Received deviceId from Email app: null
03-04 11:00:23.171: DEBUG/ExchangeService(670): !!! deviceId unknown; stopping self and retrying
03-04 11:00:28.223: DEBUG/ExchangeService(670): !!! EAS ExchangeService, onStartCommand, startingUp = false, running = false
03-04 11:00:28.233: WARN/ActivityManager(277): Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
03-04 11:00:28.233: DEBUG/ExchangeService(670): !!! Email application not found; stopping self
03-04 11:00:28.254: WARN/ActivityManager(277): Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
03-04 11:00:28.254: ERROR/ActivityThread(670): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d3ebb8 that was originally bound here
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d3ebb8 that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
at android.app.ContextImpl.bindService(ContextImpl.java:1418)
at android.app.ContextImpl.bindService(ContextImpl.java:1407)
at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
03-04 11:00:28.264: ERROR/StrictMode(670): null
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d3ebb8 that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
at android.app.ContextImpl.bindService(ContextImpl.java:1418)
at android.app.ContextImpl.bindService(ContextImpl.java:1407)
at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
03-04 11:00:28.274: WARN/ActivityManager(277): Unbind failed: could not find connection for android.os.BinderProxy@4109ae40
03-04 11:00:28.274: ERROR/ActivityThread(670): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf11b8 that was originally bound here
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf11b8 that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
at android.app.ContextImpl.bindService(ContextImpl.java:1418)
at android.app.ContextImpl.bindService(ContextImpl.java:1407)
at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
03-04 11:00:28.294: ERROR/StrictMode(670): null
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf11b8 that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
at android.app.ContextImpl.bindService(ContextImpl.java:1418)
at android.app.ContextImpl.bindService(ContextImpl.java:1407)
at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
03-04 11:00:28.294: WARN/ActivityManager(277): Unbind failed: could not find connection for android.os.BinderProxy@41067308
03-04 11:00:40.103: WARN/ProcessStats(277): Skipping unknown process pid 4067
最佳答案
if (cursor!=null)
cursor.moveToFirst();
Rating rating=new Rating((cursor.getString(0)),cursor.getString(1));
return rating;
转到:
Rating rating = null;
if (cursor.moveToFirst()) {
rating = new Rating((cursor.getString(0)),cursor.getString(1));
}
cursor.close();
return rating;