尝试将数据添加到sqlite数据库时,Android应用程序崩溃

时间:2017-05-02 07:13:21

标签: java android xml sqlite android-studio

所以我创建了一个我检查过的sqlite数据库表。但是每次我尝试添加数据时应用程序都会崩溃。我有另一个类似的表,以相同的方式实现,工作得很好,所以我猜测问题是在按钮的某个地方?

我的数据库课程:

package com.example.drivopro.drivopro;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;    
import java.util.ArrayList;
import java.util.List;


public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "Company.db";

    //My company table
    public static final String TABLE_ME = "TABLE_ME";
    public static final String MY_ID = "ID";
    public static final String MY_NAME = "NAME";
    public static final String MY_ADDRESS = "ADDRESS";
    public static final String MY_ZIP = "ZIPCODE";
    public static final String MY_CITY = "CITY";
    public static final String MY_PHONE = "PHONE";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {         
        db.execSQL("CREATE TABLE " + TABLE_ME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,ADDRESS TEXT,ZIPCODE TEXT,CITY TEXT,PHONE TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ME);
        onCreate(db);
    }

    public boolean insertMyCompany(String name, String address, String zipcode, String city, String phone) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(MY_NAME, name);
        contentValues.put(MY_ADDRESS, address);
        contentValues.put(MY_ZIP, zipcode);
        contentValues.put(MY_CITY, city);
        contentValues.put(MY_PHONE, phone);
        long result = db.insert(TABLE_ME, null, contentValues);
        if (result == -1)
            return false;
        else
            return true;
    }        
}

活动I'我正在使用:

package com.example.drivopro.drivopro;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ModifyCompanyInfo extends AppCompatActivity {

    DatabaseHelper myDb;
    Button btnsave;
    EditText changeName, changeAddress, changeZip, changeCity, changePhone;

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

        btnsave = (Button)findViewById(R.id.saveComp);
        changeName = (EditText)findViewById(R.id.myName);
        changeAddress = (EditText)findViewById(R.id.myAddress);
        changeZip = (EditText)findViewById(R.id.myZip);
        changeCity = (EditText)findViewById(R.id.myCity);
        changePhone = (EditText)findViewById(R.id.myPhone);

        addData();
    }

    public void addData(){
        btnsave.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isInserted = myDb.insertMyCompany(changeName.getText().toString(),
                                changeAddress.getText().toString(),
                                changeZip.getText().toString(),
                                changeCity.getText().toString(),
                                changePhone.getText().toString());
                        if(isInserted)
                            Toast.makeText(ModifyCompanyInfo.this, "Tietoja muutettu", Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(ModifyCompanyInfo.this, "Mitään ei tapahtunut", Toast.LENGTH_LONG).show();
                    }
                }
        );
    }
}

和XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.drivopro.drivopro.ModifyCompanyInfo">

    <EditText
        android:id="@+id/myName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Nimi"
        android:inputType="textPersonName"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.084" />

    <EditText
        android:id="@+id/myAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Osoite"
        android:inputType="textPersonName"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintTop_toBottomOf="@+id/myName" />

    <EditText
        android:id="@+id/myZip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Postinumero"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/myAddress"
        android:layout_marginBottom="-1dp"
        app:layout_constraintVertical_bias="0.0" />

    <EditText
        android:id="@+id/myCity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Kaupunki"
        android:inputType="textPersonName"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="7dp"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/myZip"
        app:layout_constraintVertical_bias="0.0" />

    <EditText
        android:id="@+id/myPhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="142dp"
        android:ems="10"
        android:hint="Puhelinnumero"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/myCity"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/cancelChanges"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="poistu"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.75"
        app:layout_constraintVertical_bias="0.691" />

    <Button
        android:id="@+id/saveComp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="Save"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.253"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.691" />
</android.support.constraint.ConstraintLayout>

任何帮助表示赞赏。提前谢谢!

1 个答案:

答案 0 :(得分:0)

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

    myDb = new DatabaseHelper(this); //Intialize the database object
}