Subhendu Roy Subhendu Roy - 4 months ago 31
Android Question

Can you please figure out the error with my code?

Basically, In this app i am trying to find out the doctor's list of a particular nursing home.In my 2nd activity there are two Button one is for User another is for Admin. User can visualize the Doctor's list with Specialization, whatever Admin will add.

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() {

}


In
MainActivity4.java
data is adding .which is connected through an Intent to this activity.

The Problem is occur when i am using Spinner in this activity. The line using String query is making the problem. If i am using this line with out spinner it will works.Kindly go trough the code once again if you will find out any error or any line which will be needed to include.

I am using Android Studion , API 19 (Jellybean).

Whenever I am trying to click the User button it shows that Unfortunately your app has stopped working. Following error shows in 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)

Answer

CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)

Your cursor doesn't contain any data

You need to validate Cursor. If your cursor count is more then 0 then and then only you need to fetch data from cursor.

So Change like this

 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);

                        }