mardi 28 juillet 2015

Android Fatal Exception Main and CursorOutOfIndexBounds [duplicate]

This question already has an answer here:

we are having a problem on checking whether the database field is empty or not. and we keep getting the error..

So our objective is: Add password if no record is found in DB. If record is found, it will crosscheck with the text field for login.

Creating the database and column is successfull. only checking is causing problems..

Thanks a lot!

  public class Front_Page extends Activity {

    EditText  getPassword;
    DBHelper dbHelper;
    Button getTrack;    


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_front__page);

          dbHelper = new DBHelper();



        Button buttonTrack = (Button)findViewById(R.id.frontTrackbtn);
        buttonTrack.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
            getPassword = (EditText) findViewById(R.id.frontTxtPass);
            String Password = getPassword.getText().toString();
            dbHelper = new DBHelper();


            dbHelper.create_OpenDatabase("sdcard/UserNameData.db");
            String sql="create table if not exists UserInfo (Password text)";
            dbHelper.createTable(sql);


            dbHelper.create_OpenDatabase("sdcard/UserNameData.db");
            String sql2 = "SELECT * FROM UserInfo";
            Cursor cursor=  dbHelper.queryTableCursor(sql2);

            String Pass = cursor.getString(0); 
            if(Pass.equals(null))
            {
                dbHelper.create_OpenDatabase("sdcard/UserNameData.db");
                String sql3 = "insert into UserInfo (Password) values('"+Password+"')";
                dbHelper.updateTable(sql3);
                Intent account = new Intent(Front_Page.this, Account.class);
                account.putExtra("passPass",Password);
                startActivity(account);
            }
            else if (Pass.equals(Password))
            {
                Intent account = new Intent(Front_Page.this, Account.class);
                account.putExtra("passPass",Password);
                startActivity(account);
            }
            else if (!Pass.equals(Password))
            {
                Toast.makeText(Front_Page.this, "Invalid Password", Toast.LENGTH_LONG).show();
                return;
            }
            else
            {
                Toast.makeText(Front_Page.this, "Please enter password", Toast.LENGTH_LONG).show();
                return;
            }


            }



        });


    }

our Layout:

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/simpleblue"

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.expensetracker.Front_Page" >

<TextView
    android:id="@+id/TitleFront"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="44dp"
    android:text="@string/front_pass"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/white"
    android:textColorLink="@android:color/white" />

<Button
    android:id="@+id/frontTrackbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/TitleFront"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="136dp"
    android:text="@string/frontLogin" />

<EditText
    android:id="@+id/frontTxtPass"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/TitleFront"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:ems="10"
    android:inputType="textPassword" >

    <requestFocus />
</EditText>

and this is the error we keep getting:

07-28 05:02:47.157: I/System.out(2663): create table if not exists UserInfo (Password text)
07-28 05:02:47.228: I/System.out(2663): SELECT * FROM UserInfo
07-28 05:02:47.238: D/AndroidRuntime(2663): Shutting down VM
07-28 05:02:47.238: W/dalvikvm(2663): threadid=1: thread exiting with uncaught exception (group=0x41465730)
07-28 05:02:47.298: E/AndroidRuntime(2663): FATAL EXCEPTION: main
07-28 05:02:47.298: E/AndroidRuntime(2663): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
07-28 05:02:47.298: E/AndroidRuntime(2663):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)

Aucun commentaire:

Enregistrer un commentaire