模拟器运行但应用程序在按钮单击时结束

时间:2013-08-05 10:55:14

标签: android

  

我的程序有一个活动和一个数据库类。有一个用于从数据库中检索数据的按钮。当我按下按钮时应用程序结束。我认为数据库中的一些错误是querry.pls帮我解决的。给出了logcat。   FirstActivity.java

package example.showevent1;

import java.util.Calendar;

import android.os.Bundle;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.*;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class FirstActivity extends FragmentActivity implements OnItemSelectedListener {
    /** Called when the activity is first created. */

    classdbOpenHelper eventsData;
    classdbOpenHelper eventsData1;

     TextView userSelection;
     Button okButton;
     Button addButton;

     Button change_date_but;
     TextView date;
     TextView show;
     EditText edittext;
      public static final int Date_dialog_id = 1;
      private int mYear;
      private int mMonth;
      private int mDay;

    private static final String[] items={"Yalahanka","Rajai nagar","Sivaji Nagar","Koramangala","RT Nagar", "Banashankari","Yashwanthpura","Hebbal"};



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
         okButton = (Button)findViewById(R.id.button2);
         addButton = (Button)findViewById(R.id.button3);

         change_date_but = (Button)findViewById(R.id.button1);
         date = (TextView)findViewById(R.id.textView2);//KEY_DATE
         userSelection=(TextView)findViewById(R.id.textView1);//KEY_DESC
         edittext=(EditText)findViewById(R.id.editText1);//KEY_EVENT
         show=(TextView)findViewById(R.id.textView5);

         Spinner my_spin=(Spinner)findViewById(R.id.spinner1);
        my_spin.setOnItemSelectedListener(this);
        ArrayAdapter aa=new ArrayAdapter(this, android.R.layout.simple_spinner_item,items);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        my_spin.setAdapter(aa);

        okButton.setOnClickListener(new clicker());
        addButton.setOnClickListener(new getclicker());

        eventsData = new classdbOpenHelper(this);

        change_date_but.setOnClickListener(new View.OnClickListener() {



                   @Override
                   public void onClick(View v) {
                     DatePickerDialog DPD = new DatePickerDialog(
                   FirstActivity.this, mDateSetListener, mYear, mMonth,mDay);
                                DPD.show();
               }
                  });
        final Calendar c = Calendar.getInstance();
              mYear = c.get(Calendar.YEAR);
              mMonth = c.get(Calendar.MONTH);
              mDay = c.get(Calendar.DAY_OF_MONTH);

              updateDisplay();
    }
     @Override
         @Deprecated

         protected void onPrepareDialog(int id, Dialog dialog) {
          // TODO Auto-generated method stub
       super.onPrepareDialog(id, dialog);

          ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);

         }

         private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

       public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
           mYear = year;
           mMonth = monthOfYear;
       mDay = dayOfMonth;
           updateDisplay();
          }
         };

         private void updateDisplay() {
       // TODO Auto-generated method stub
          date.setText(new StringBuilder()
            // Month is 0 based so add 1
         .append(mMonth + 1).append("-").append(mDay).append("-")
            .append(mYear));
         }







    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3) {
        userSelection.setText(items[pos]);
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
        userSelection.setText("");
    }



    class clicker implements Button.OnClickListener {
        public void onClick(View v) {
            String datevalue = date.getText().toString();
            String Userselectvalue = userSelection.getText().toString();
            String Userevent = edittext.getText().toString();
            SQLiteDatabase  db = eventsData.getWritableDatabase();

            ContentValues cv = new ContentValues();
            cv.put(classdbOpenHelper.KEY_DESC, Userselectvalue);
            cv.put(classdbOpenHelper.KEY_EVENT, Userevent);
            cv.put(classdbOpenHelper.KEY_DATE,datevalue);
            db.insert(classdbOpenHelper.DATABASE_TABLE, null, cv);
            db.close();
        }


    public void onDestroy() {
      eventsData.close();
    }

}
    class getclicker implements Button.OnClickListener {
        public void onClick(View v) {
            String datevalue = date.getText().toString();
            String Userselectvalue = userSelection.getText().toString();

            String showevent = eventsData.getContact(datevalue,Userselectvalue);
            show.setText(showevent);
        }

    }

}

classdbOpenHelper.java

package example.showevent1;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.TextView;


public class classdbOpenHelper extends SQLiteOpenHelper {

    public static final String KEY_ROWID = "id";
    public static final String KEY_DESC = "countdesc";
    public static final String KEY_EVENT = "countevent";
    public static final String KEY_DATE = "countdate";

    public static final String DATABASE_NAME= "countdb";
    public static final String DATABASE_TABLE = "countable";
    public static final int DATABASE_VERSION = 1;


    public classdbOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + DATABASE_TABLE + "("
                + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DESC + " TEXT, " + KEY_EVENT + " TEXT, " + KEY_DATE + " TEXT " + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXITS " + DATABASE_TABLE);
        onCreate(db);

    }

    public String getContact(String datevalue,String Userselectvalue) {
        String selection = classdbOpenHelper.KEY_DESC + " = Userselectvalue"  
                +" AND " + classdbOpenHelper.KEY_DATE + " datevalue";
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(classdbOpenHelper.DATABASE_TABLE, 
                new String[] {classdbOpenHelper.KEY_EVENT }, selection, 
                null, null, null, null);

      String  place = cursor.getString(0 );
        return place;
    }
}
  

logcat的

08-05 11:21:13.327: E/AndroidRuntime(827): FATAL EXCEPTION: main
08-05 11:21:13.327: E/AndroidRuntime(827): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
08-05 11:21:13.327: E/AndroidRuntime(827):  at example.showevent1.classdbOpenHelper.getContact(classdbOpenHelper.java:59)
08-05 11:21:13.327: E/AndroidRuntime(827):  at example.showevent1.FirstActivity$getclicker.onClick(FirstActivity.java:163)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.view.View.performClick(View.java:4204)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.view.View$PerformClick.run(View.java:17355)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.os.Handler.handleCallback(Handler.java:725)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.os.Looper.loop(Looper.java:137)
08-05 11:21:13.327: E/AndroidRuntime(827):  at android.app.ActivityThread.main(ActivityThread.java:5041)
08-05 11:21:13.327: E/AndroidRuntime(827):  at java.lang.reflect.Method.invokeNative(Native Method)
08-05 11:21:13.327: E/AndroidRuntime(827):  at java.lang.reflect.Method.invoke(Method.java:511)
08-05 11:21:13.327: E/AndroidRuntime(827):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-05 11:21:13.327: E/AndroidRuntime(827):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-05 11:21:13.327: E/AndroidRuntime(827):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

String  place = cursor.getString(0 );

您没有正确访问光标。首先使用if(cursor.moveToFirst())检查游标是否为空,然后使用cursor.moveToFirst()将光标移动到第一行。然后,您可以使用cursor.getString(cursor.getColumnIndex(COLUMN_NAME))检索值并将其分配给您需要的字符串。

Logcat显示,`

08-05 11:21:13.327: E/AndroidRuntime(827): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0`, meaning that you have a problem with cursor operation.

替换

String  place = cursor.getString(0 );`

使用

if(cursor.moveToFirst())
cursor.moveToFirst();

String place = cursor.getString(cursor.getColumIndex(COLUMN_NAME))

将COLUMN_NAME替换为数据库中的column_name