English 中文(简体)
使用此类时应用程序关闭
原标题:Application Closes when using this class

当我打开这个类时,我的应用程序就会关闭。logcat显示第28行有问题,但我从这里这个类的代码如下

public class WorkoutProgress extends ListActivity {
private DataBaseHelper datasource;
TextView goal; 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.    FLAG_FULLSCREEN);
setContentView(R.layout.progress);
goal = (TextView)findViewById(R.id.goal);
datasource = new DataBaseHelper(this);
datasource.open();
Cursor c = datasource.getAllGoals();
startManagingCursor(c);
if(c.getCount() > 0)
{
String g = c.getString(1);
int g2= c.getInt(2);
int g3 = c.getInt(3);
String Goal = (g+g2+g3);
goal.setText(Goal);
}
fillData();
datasource.close();
}
private void fillData() {
// Get all of the notes from the database and create the item list
   Cursor c = datasource.getAllActs();
    startManagingCursor(c);
    String[] from = new String[] {DataBaseHelper.KEY_DATE,  
    DataBaseHelper.KEY_STEPS,DataBaseHelper.KEY_CALs };
    int[] to = { R.id.code, R.id.Days, R.id.BMI };
    SimpleCursorAdapter notes = new SimpleCursorAdapter (this, R.layout.notes_row, c,  from, to);
   setListAdapter(notes);               
 }

     /*public void add(View view)
     {
         //Do nothing
     }
     public void delete(View view)
     {
             datasource.open();
             datasource.deleteFirst();
             fillData();
             datasource.close();
     }*/
}

logcat是

05-27 17:39:08.531: E/AndroidRuntime(369): FATAL EXCEPTION: main
05-27 17:39:08.531: E/AndroidRuntime(369): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.b00348312.workout/com.b00348312.workout.WorkoutProgress}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
05-27 17:39:08.531: E/AndroidRuntime(369):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-27 17:39:08.531: E/AndroidRuntime(369):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-27 17:39:08.531: E/AndroidRuntime(369):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-27 17:39:08.531: E/AndroidRuntime(369):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-27 17:39:08.531: E/AndroidRuntime(369):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 17:39:08.531: E/AndroidRuntime(369):  at android.os.Looper.loop(Looper.java:123)
05-27 17:39:08.531: E/AndroidRuntime(369):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-27 17:39:08.531: E/AndroidRuntime(369):  at java.lang.reflect.Method.invokeNative(Native Method)
05-27 17:39:08.531: E/AndroidRuntime(369):  at java.lang.reflect.Method.invoke(Method.java:521)
05-27 17:39:08.531: E/AndroidRuntime(369):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-27 17:39:08.531: E/AndroidRuntime(369):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-27 17:39:08.531: E/AndroidRuntime(369):  at dalvik.system.NativeStart.main(Native Method)
05-27 17:39:08.531: E/AndroidRuntime(369): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
05-27 17:39:08.531: E/AndroidRuntime(369):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
05-27 17:39:08.531: E/AndroidRuntime(369):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
05-27 17:39:08.531: E/AndroidRuntime(369):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
05-27 17:39:08.531: E/AndroidRuntime(369):  at com.b00348312.workout.WorkoutProgress.onCreate(WorkoutProgress.java:28)
05-27 17:39:08.531: E/AndroidRuntime(369):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-27 17:39:08.531: E/AndroidRuntime(369):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-27 17:39:08.531: E/AndroidRuntime(369):  ... 11 more
问题回答

索引-请求1,大小为1-这是因为游标指向第一个数据行之前。你需要使用c.moveToFirst()。。。

if(c.getCount() > 0)
{
    c.moveToFirst()
    String g = c.getString(1);
    ...
}

游标最初总是被设置为指向第一个结果(索引-1)之前,因为并非所有查询都会返回任何数据。如果有结果,第一个结果(数据行)位于索引0,这就是为什么你需要使用moveToFirst()或其他游标moveTo。。。方法在尝试从游标检索数据之前





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签