尝试在按钮单击时在sqlite中保存数据时出现java.lang.ClassCastException错误

时间:2014-03-06 06:38:51

标签: android

我在活动中有标签,当我点击按钮打开标签视图时它工作正常。现在在字段中输入数据后,当我点击按钮保存它时,它给了我错误。可能是我在sqlite数据库构造函数中有错误但不确定。有人请帮忙:     这是我的选项卡代码:

package com.example.doctormanager;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

public class General extends Activity  {
    Context context;
    private Button proceed;
    private EditText patient_name,mobile,address,email,dob,age;
    ListView template_list;
    Intent data_intent;
    String name="";


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.patient_general);
            data_intent=getIntent();
            name=data_intent.getStringExtra("name");

            patient_name=(EditText)findViewById(R.id.txt17);
            patient_name.setText(""+name);
            mobile=(EditText)findViewById(R.id.txt23);
            address=(EditText)findViewById(R.id.txt21);
            email=(EditText)findViewById(R.id.txt19);
            dob=(EditText)findViewById(R.id.txt25);
            age=(EditText)findViewById(R.id.txt27);
            ImageButton save= (ImageButton)findViewById(R.id.ib13);

            save.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub


                    SqlHandler db = new SqlHandler(this);
            String p_name = patient_name.getText().toString();
            String p_mobile = mobile.getText().toString();
            String p_addr = address.getText().toString();
            String p_email = email.getText().toString();
            String p_dob = dob.getText().toString();
            String p_age = age.getText().toString();


            /**
             * CRUD Operations
             * */
            // Inserting Contacts
            Log.d("Insert: ", "Inserting .."); 
            db.addContact(new Patient_entery(0, p_name, p_mobile, p_addr, p_email, p_dob, p_age, null));  
                }
            });

}



}

这是针对数据库的:

package com.example.doctormanager;

    import java.util.ArrayList;
    import java.util.List;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.view.View.OnClickListener;

    public class SqlHandler extends SQLiteOpenHelper {

        public SqlHandler(Context context, String name, CursorFactory factory,
                int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
        }


        // All Static variables
        // Database Version
        private static final int DATABASE_VERSION = 1;

        // Database Name
        private static final String DATABASE_NAME = "doctormanager";

        // Contacts table name
        private static final String TABLE_PATIENT_GENERAL= "general";

        // Contacts Table Columns names
        private static final String KEY_ID = "id";
        private static final String KEY_PATIENT_NAME = "patient_name";
        private static final String KEY_EMAIL = "patient_email";
        private static final String KEY_PATIENT_ADDR = "patient_addr";

        private static final String KEY_PATIENT_CONTACT_NUMBER = "patient_contact_number";
        private static final String KEY_PATIENT_DOB = "patient_dob";
        private static final String KEY_PATIENT_AGE = "patient_age";
        private static final String KEY_PATIENT_SEX = "patient_sex";

        public SqlHandler (OnClickListener onClickListener) {
            super((Context) onClickListener, DATABASE_NAME, null, DATABASE_VERSION);
            }


    //    public SqlHandler(OnClickListener onClickListener) {
    //      // TODO Auto-generated constructor stub
    //  }

        // Creating Tables
        @Override
        public void onCreate(SQLiteDatabase db) {
            String CREATE_PATIENT_TABLE = "CREATE TABLE " + TABLE_PATIENT_GENERAL + "("
                    + KEY_ID + " INTEGER PRIMARY KEY," + KEY_PATIENT_NAME + " TEXT,"
                    + KEY_PATIENT_CONTACT_NUMBER + " TEXT," + KEY_EMAIL +"TEXT," + KEY_PATIENT_ADDR + "TEXT," 
                    + KEY_PATIENT_DOB + "TEXT," + KEY_PATIENT_AGE + "TEXT," + KEY_PATIENT_SEX + "TEXT " + ")";
            db.execSQL(CREATE_PATIENT_TABLE);
        }

        // Upgrading database
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // Drop older table if existed
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_PATIENT_GENERAL);

            // Create tables again
            onCreate(db);
        }

        /**
         * All CRUD(Create, Read, Update, Delete) Operations
         */

        // Adding new contact
        void addContact(Patient_entery patient) {
            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(KEY_PATIENT_NAME, patient.getName()); // Contact Name
            values.put(KEY_PATIENT_CONTACT_NUMBER, patient.getPhoneNumber()); // Contact Phone
            values.put(KEY_EMAIL, patient.getPatientEmail()); // Contact Name
            values.put(KEY_PATIENT_ADDR, patient.getAddress()); // Contact Phone

            values.put(KEY_PATIENT_DOB, patient.getDob()); // Contact Name
            values.put(KEY_PATIENT_AGE, patient.getAge()); // Contact Phone

            values.put(KEY_PATIENT_SEX, patient.getSex()); // Contact Name



            // Inserting Row
            db.insert(TABLE_PATIENT_GENERAL, null, values);
            db.close(); // Closing database connection
        }

        // Getting single contact
        Patient_entery getPatient(int id) {
            SQLiteDatabase db = this.getReadableDatabase();

            Cursor cursor = db.query(TABLE_PATIENT_GENERAL, new String[] { KEY_ID,
                    KEY_PATIENT_NAME, KEY_PATIENT_CONTACT_NUMBER, KEY_EMAIL, KEY_PATIENT_ADDR, KEY_PATIENT_DOB, KEY_PATIENT_AGE, KEY_PATIENT_SEX }, KEY_ID + "=?",
                    new String[] { String.valueOf(id) }, null, null, null, null);
            if (cursor != null)
                cursor.moveToFirst();

            Patient_entery patient = new Patient_entery(Integer.parseInt(cursor.getString(0)),
                    cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7));
            // return contact
            return patient;
        }
public String getAllStringValues() {
        ArrayList<String> yourStringValues = new ArrayList<String>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor result = db.query(true, TABLE_PATIENT_GENERAL,
                new String[] { KEY_PATIENT_NAME }, null, null, null, null,
                null, null);

        if (result.moveToFirst()) {
            do {
                yourStringValues.add(result.getString(result
                        .getColumnIndex(KEY_PATIENT_NAME)));
            } while (result.moveToNext());
        } else {
            return null;
        }
        return KEY_PATIENT_NAME;
    }

}

这是XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 

    android:background="@drawable/login_back">



    <LinearLayout 
        android:layout_height="30dip"
        android:layout_width="wrap_content"
        android:orientation="horizontal"
        >

  <TextView 
      android:id="@+id/txt16"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Name: "
      android:textSize="20dip"
      android:textColor="#FFFFFF"/>

  <EditText
      android:id="@+id/txt17"
      android:layout_width="200dp"
      android:layout_height="20dp"
      android:background="@drawable/edit_text_lines"
      android:ems="10"
      android:textSize="10dip" />

</LinearLayout>
    <LinearLayout 
        android:layout_height="40dip"
        android:layout_width="wrap_content"
        android:orientation="horizontal">

  <TextView 
      android:id="@+id/txt18"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Email: "
      android:textSize="20dip"
      android:textColor="#FFFFFF"/>
     <EditText 
      android:id="@+id/txt19"
      android:layout_width="200dp"
      android:layout_height="20dp"
      android:text=""
      android:textSize="10dip"

     android:background="@drawable/edit_text_lines"
      />
</LinearLayout>


    <LinearLayout 
        android:layout_height="40dip"
        android:layout_width="wrap_content"
        android:orientation="horizontal">

  <TextView 
      android:id="@+id/txt20"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Address: "
      android:textSize="20dip"
      android:textColor="#FFFFFF"/>
     <EditText 
      android:id="@+id/txt21"
     android:layout_width="200dp"
      android:layout_height="20dp"
      android:text=""
      android:textSize="10dip"

     android:background="@drawable/edit_text_lines" />
</LinearLayout>
<LinearLayout 
        android:layout_height="40dip"
        android:layout_width="wrap_content"
        android:orientation="horizontal">

  <TextView 
      android:id="@+id/txt22"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Phone No.: "
      android:textSize="20dip"
      android:textColor="#FFFFFF"/>
     <EditText
      android:id="@+id/txt23"
      android:layout_width="200dp"
      android:layout_height="20dp"
      android:text=""
      android:textSize="10dip"

     android:background="@drawable/edit_text_lines"/>
</LinearLayout>
<LinearLayout 
        android:layout_height="40dip"
        android:layout_width="wrap_content"
        android:orientation="horizontal">

  <TextView 
      android:id="@+id/txt24"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="DOB: "
      android:textSize="20dip"
      android:textColor="#FFFFFF"/>
     <EditText
      android:id="@+id/txt25"
      android:layout_width="200dp"
      android:layout_height="20dp"
      android:text=""
      android:textSize="10dip"

     android:background="@drawable/edit_text_lines"/>
</LinearLayout>
<LinearLayout 
        android:layout_height="40dip"
        android:layout_width="wrap_content"
        android:orientation="horizontal">

  <TextView 
      android:id="@+id/txt26"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Age: "
      android:textSize="20dip"
      android:textColor="#FFFFFF"/>
     <EditText
      android:id="@+id/txt27"
      android:layout_width="200dp"
      android:layout_height="20dp"
      android:text=""
      android:textSize="10dip"

     android:background="@drawable/edit_text_lines"/>
</LinearLayout>
<LinearLayout 
        android:layout_height="40dip"
        android:layout_width="wrap_content"
        android:orientation="horizontal">

  <TextView 
      android:id="@+id/stxt27"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Sex: "
      android:textSize="20dip"
      android:textColor="#FFFFFF"/>
     <RadioGroup
        android:id="@+id/radioSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:orientation="horizontal"
       android:textColor="#FFFFFF" >

        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male" 
            android:textColor="#FFFFFF"/>

        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female" 
           android:textColor="#FFFFFF" />
         </RadioGroup>

         </LinearLayout>


  <TextView 
      android:id="@+id/txt28"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Patient/Family History:  "
      android:textSize="20dip"
      android:textColor="#FFFFFF"/>

     <EditText 
      android:id="@+id/edttxt27"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text=""
     android:textColor="#FFFFFF" />
     <ImageButton 
    android:id="@+id/ib14"
    android:src="@drawable/template" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:paddingRight="1dip"


    />

<ImageButton 
    android:id="@+id/ib13"
    android:src="@drawable/next_visit" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"

    android:layout_gravity="center_horizontal"
    />

</LinearLayout>

感谢先生。

1 个答案:

答案 0 :(得分:2)

您需要在Activity context上传递SqlHandler,如:

SqlHandler db = new SqlHandler(youractivity.this);
{p} Button onclick()

中的

Remove OnClickListener onClickListener()来自SqlHanlder Constructer,例如:

 public SqlHandler (Context con) {
    super(con, DATABASE_NAME, null, DATABASE_VERSION);
    }