lundi 2 mai 2016

Error when trying save data in sqlite

Hi guys i just got this error when trying to create database and save data in the table Please Help ( Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()')

Database.java

public class Database extends SQLiteOpenHelper   {

final static String Database_Name = "Jordangym.db";
final static String Table_Name = "Body";


public Database(Context context) {
    super(context, Database_Name, null, 1);
    SQLiteDatabase db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("Create table "+Table_Name+"(Date TEXT , Height TEXT , Wieght TEXT , Biceps TEXT , Chest TEXT , Waiste TEXT )");


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS"+Table_Name);
    onCreate(db);

}

public boolean insertdata (String Height ,String Wieght, String Biceps, String Chest , String Waiste ){

    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("Date", System.currentTimeMillis());
    values.put("Height",Height);
    values.put("Wieght",Wieght);
    values.put("Biceps",Biceps);
    values.put("Chest",Chest);
    values.put("Waiste",Waiste);

    long result=db.insert(Table_Name,null,values);

    if (result==-1){
        return false;
    }
    else {
        return true;
    }


}

}

BodyMeasure.java

public class BodyMeasure extends AppCompatActivity {

EditText EdtHeight, EdtWieght, EdtChest, EdtWaiste, EdtBicep;
Button buHeight;
Database mydb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.body_measure);

    mydb = new Database(this);

    EdtBicep = (EditText)findViewById(R.id.edtBiceps);
    EdtChest = (EditText)findViewById(R.id.edtChest);
    EdtHeight = (EditText)findViewById(R.id.edtHeight);
    EdtWieght = (EditText)findViewById(R.id.edtWight);
    EdtWieght =(EditText)findViewById(R.id.edtWaiste);

    buHeight = (Button)findViewById(R.id.buHeight);
    BuHeightAdd();

}


public void BuHeightAdd (){
    buHeight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           boolean IsInserted = mydb.insertdata(
                   EdtHeight.getText().toString(),
                   EdtWieght.getText().toString(),
                   EdtBicep.getText().toString(),
                   EdtChest.getText().toString(),
                   EdtWaiste.getText().toString());


            if (IsInserted == true )
                Toast.makeText(BodyMeasure.this,"Done",Toast.LENGTH_LONG).show();
            else
                Toast.makeText(BodyMeasure.this,"Error",Toast.LENGTH_LONG).show();
        }

    });
}

}

body_measure.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://ift.tt/nIICcg"

    android:layout_height="500dp"
    android:layout_width="fill_parent"
    >

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    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.qais.jordangym.BodyMeasure">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="80dp">

        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="80dp">

        <ImageView
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:background="@drawable/appicon"/>
        <TextView
            android:id="@+id/txtHeight"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:text="Height"
            android:layout_marginLeft="80dp"
            android:gravity="center"/>
        <EditText
            android:id="@+id/edtHeight"
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:layout_alignLeft="@+id/txtHeight"
            android:layout_alignRight="@+id/buHeight"/>
            <Button
                android:id="@+id/buHeight"
                android:layout_width="50dp"
                android:layout_height="80dp"
                android:text="S"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"

                />

        </RelativeLayout>

    </LinearLayout>
    <RelativeLayout
        android:layout_marginTop="90dp"
        android:layout_width="fill_parent"
        android:layout_height="80dp">
        <ImageView
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:background="@drawable/appicon"/>
        <TextView
            android:id="@+id/txtWeight"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:text="Wieght"
            android:layout_marginLeft="80dp"
            android:gravity="center"/>
        <EditText
            android:id="@+id/edtWight"
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:layout_marginLeft="135dp"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_marginTop="180dp"
        android:layout_width="fill_parent"
        android:layout_height="80dp">
        <ImageView
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:background="@drawable/appicon"/>
        <TextView
            android:id="@+id/txtBiceps"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:text="Biceps"
            android:layout_marginLeft="80dp"
            android:gravity="center"/>
        <EditText
            android:id="@+id/edtBiceps"
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:layout_marginLeft="135dp"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_marginTop="270dp"
        android:layout_width="fill_parent"
        android:layout_height="80dp">
        <ImageView
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:background="@drawable/appicon"/>
        <TextView
            android:id="@+id/txtChest"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:text="Chest"
            android:layout_marginLeft="80dp"
            android:gravity="center"/>
        <EditText
            android:id="@+id/edtChest"
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:layout_marginLeft="135dp"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_marginTop="360dp"
        android:layout_width="fill_parent"
        android:layout_height="80dp">
        <ImageView
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:background="@drawable/appicon"/>
        <TextView
            android:id="@+id/txtWaiste"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:text="Waiste"
            android:layout_marginLeft="80dp"
            android:gravity="center"/>
        <EditText
            android:id="@+id/edtWaiste"
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:layout_marginLeft="135dp"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_marginTop="450dp"
        android:layout_width="fill_parent"
        android:layout_height="80dp">
        <ImageView
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:background="@drawable/appicon"/>
        <TextView
            android:id="@+id/txtOptional"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:text="Optional"
            android:layout_marginLeft="80dp"
            android:gravity="center"/>
    </RelativeLayout>

</RelativeLayout>

</ScrollView>

Aucun commentaire:

Enregistrer un commentaire