你能用我的代码找出错误吗?

时间:2016-07-15 04:04:27

标签: android database android-layout cursor indexoutofboundsexception

基本上,在这个应用程序中,我试图找出医生的特定疗养院的名单。在我的第二个活动有两个按钮一个用户另一个用于管理员。用户可以通过专业化来显示医生的列表,无论管理员将添加什么。

        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中,数据正在添加.which通过Intent连接到此活动。

当我在此活动中使用Spinner时会出现问题。使用String查询的行正在解决问题。如果我在旋转器中使用此行,它将工作。如果您发现任何错误或需要包含的任何行,请再次通过代码。

我正在使用Android Studion,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)

2 个答案:

答案 0 :(得分:0)

  

CursorIndexOutOfBoundsException:请求索引0,大小为0               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)

您的光标不包含任何数据

您需要验证Cursor。如果您的光标数超过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);

                        }

答案 1 :(得分:0)

您的错误Index 0 requested, with a size of 0说明了一切。 Cursor没有数据,而您正试图从第0位获取记录。

只需从代码中取消注释// if (c.moveToFirst()) {这一行即可。

否则你也可以使用Ironman's solution,两者都可以。