在sqlite中尝试保存数据时出错

时间:2016-05-02 10:49:24

标签: java android sqlite

大家好我刚尝试创建数据库并在表格中保存数据时遇到此错误请帮助

  

(尝试调用虚方法'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://schemas.android.com/apk/res/android"

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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>

2 个答案:

答案 0 :(得分:2)

请检查此 EdtWaiste 未初始化其空

EdtWieght = (EditText)findViewById(R.id.edtWight);
EdtWieght =(EditText)findViewById(R.id.edtWaiste);



to 
EdtWieght = (EditText)findViewById(R.id.edtWight);
EdtWaiste =(EditText)findViewById(R.id.edtWaiste);

答案 1 :(得分:0)

在BodyMeasure中,

你应该初始化EdtWaiste。

a <- array(1:16, c(2, 2, 2))
#, , 1
#
#     [,1] [,2]
#[1,]    1    3
#[2,]    2    4
#
#, , 2
#
#     [,1] [,2]
#[1,]    5    7
#[2,]    6    8

rowSums(a, dims = 2)
#     [,1] [,2]
#[1,]    6   10
#[2,]    8   12

你的错误是,EdtWieght被初始化两次