基本上,在这个应用程序中,我试图找出特定疗养院的医生名单。在我的第二个 Activity 中,有两个按钮,一个供用户使用,另一个供管理员使用。用户可以看到具有特化的医生列表,无论管理员将添加什么。
package com.example.lenovo.finalproject;
import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity5 extends Activity {
private final String DB_NAME = "My_DB2";
private final String TB_NAME = "TB2";
SQLiteDatabase db;
Spinner sp;
ListView l;
String str[] = {"Orthopaedics", "Cardiologist"};
String special;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity5);
// open the database
db = this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
sp = (Spinner) findViewById(R.id.spinner3);
l = (ListView) findViewById(R.id.listView3);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
special = parent.getItemAtPosition(position).toString().trim();
Toast.makeText(getApplicationContext(), special, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// open the database
// db = this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
initListViewData();
}
private void initListViewData() {
String selectQuery = " SELECT * FROM " + TB_NAME + " where speciality " + " = " + special;// problem arising these line//
String did;
String name;
String speciality;
String time;
String fees;
Cursor c = null;
try {
ArrayList<String> listItems = new ArrayList<String>();
c = db.rawQuery(selectQuery, null);
if (c != null) {
// if (c.moveToFirst()) {
c.moveToFirst();
do {
did = c.getString(c.getColumnIndex("id"));
name = c.getString(c.getColumnIndex("name"));
speciality = c.getString(c.getColumnIndex("speciality"));
time = c.getString(c.getColumnIndex("time"));
fees = c.getString(c.getColumnIndex("fees"));
String s = did + "\n " + name + "\n " + speciality + " \n" + time + "\n " + fees;
listItems.add(s);
} while (c.moveToNext());
// }
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, listItems);
// l=(ListView)findViewById(R.id.listView1);
l.setAdapter(adapter1);
} else {
Toast.makeText(getApplicationContext(), " No records found..", Toast.LENGTH_SHORT).show();
}
} catch (SQLException se) {
se.printStackTrace();
} finally {
if (null != c) {
c.close();
}
}
}
void show() {
}
在 MainActivity4.java
中,数据正在添加。它通过 Intent 连接到此 Activity 。
问题发生在我在此 Activity 中使用 Spinner 时。使用 String 查询的行是问题所在。如果我在没有微调器的情况下使用这一行,它将起作用。如果您发现任何错误或需要包含的任何行,请再次检查代码。
我正在使用 Android Studio,API 19 (Jellybean)。
每当我尝试单击“用户”按钮时,它都会显示“很遗憾,您的应用已停止工作”。 logcat 中显示以下错误
07-14 23:42:56.945 1853-1853/com.example.lenovo.finalproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lenovo.finalproject/com.example.lenovo.finalproject.MainActivity5}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
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.lenovo.finalproject.MainActivity5.initListViewData(MainActivity5.java:90)
at com.example.lenovo.finalproject.MainActivity5.onCreate(MainActivity5.java:69)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
最佳答案
CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
您的光标不包含任何数据
您需要验证游标
。如果您的游标计数大于 0
,那么您只需要从游标中获取数据。
所以改成这样
if (c != null & c.getCount > 0) {
// if (c.moveToFirst()) {
c.moveToFirst();
do {
did = c.getString(c.getColumnIndex("id"));
name = c.getString(c.getColumnIndex("name"));
speciality = c.getString(c.getColumnIndex("speciality"));
time = c.getString(c.getColumnIndex("time"));
fees = c.getString(c.getColumnIndex("fees"));
String s = did + "\n " + name + "\n " + speciality + " \n" + time + "\n " + fees;
listItems.add(s);
}
关于android - 你能用我的代码找出错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38387686/