gpt4 book ai didi

android - 使用此类时应用程序关闭

转载 作者:行者123 更新时间:2023-11-29 22:05:59 25 4
gpt4 key购买 nike

当我打开这个类时,我的应用程序关闭。 logcat 说第 28 行有问题,但是我从关于 here 的问题中得到的代码这个类的代码在下面

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() 或其他 Cursor“moveTo...”方法之一来自 Cursor

的数据

关于android - 使用此类时应用程序关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10775995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com