Android - 将数据输入数据库时​​应用程序崩溃

时间:2016-03-16 17:21:42

标签: java android mysql database android-fragments

我昨天问过这个问题,但从来没有得到最终的解决方案。我遇到一个问题,当我输入数据插入我的数据库时,应用程序崩溃试图将其添加进去。我不确定该问题,之后多次尝试清理我的代码,没有任何工作。该错误是由我sqLiteDatabase = myDB.getWritableDatabase();中的addScoutInfo();AddScoutDataFragment.java引起的,但即使在尝试清理它之后,仍然无效。这次我希望我的所有课程都能找出问题。

AddScoutDataFragment.java

import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * A simple {@link Fragment} subclass.
 */
public class AddScoutDataFragment extends Fragment {

    Button cancelButton;
    Button addDataButton;
    DatabaseHelper myDB;
    EditText tNumber, tPoticullis, tChevalFrise, tMoat, tRamparts, tDrawbridge, tSallyPort, tRockWall, tRockTerrain, tLowBar;
    SQLiteDatabase sqLiteDatabase;

    public AddScoutDataFragment() {
        // Required empty public constructor
    } //End of AddScoutDataFragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_add_scout_data, container, false);
        view.setBackgroundColor(Color.WHITE);

        //Starts database
        myDB = new DatabaseHelper(getActivity());

        //Instantiate all editText objects
        tNumber = (EditText) view.findViewById(R.id.editNumber);
        tPoticullis = (EditText) view.findViewById(R.id.editPoticullis);
        tChevalFrise = (EditText) view.findViewById(R.id.editChevalFrise);
        tMoat = (EditText) view.findViewById(R.id.editMoat);
        tRamparts = (EditText) view.findViewById(R.id.editRamparts);
        tDrawbridge = (EditText) view.findViewById(R.id.editDrawbridge);
        tSallyPort = (EditText) view.findViewById(R.id.editSallyPort);
        tRockWall = (EditText) view.findViewById(R.id.editRockWall);
        tRockTerrain = (EditText) view.findViewById(R.id.editRockTerrain);
        tLowBar = (EditText) view.findViewById(R.id.editLowBar);

        //Adds data to ScoutFragment
        addDataButton = (Button) view.findViewById(R.id.buttonDataAdd);
        addDataButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Saves data to database
                addScoutInfo();

                //Returns to ScoutFragment
                ScoutFragment fragment = new ScoutFragment();
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.commit();
            } //End of onClick
        }); //End of setOnClickListener

        //Returns to ScoutFragment without adding any data
        cancelButton = (Button) view.findViewById(R.id.buttonCancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Returns to ScoutFragment
                ScoutFragment fragment = new ScoutFragment();
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.commit();
            } //End of onClick
        }); //End of setOnClickListener
        // Inflates the layout for this fragment
        return view;
    } //End of onCreateView

    public void addScoutInfo() {
        //Converts all editText values into strings
        String sNumber = tNumber.getText().toString();
        String sPoticullis = tPoticullis.getText().toString();
        String sChevalFrise = tChevalFrise.getText().toString();
        String sMoat = tMoat.getText().toString();
        String sRamparts = tRamparts.getText().toString();
        String sDrawbridge = tDrawbridge.getText().toString();
        String sSallyPort = tSallyPort.getText().toString();
        String sRockWall = tRockWall.getText().toString();
        String sRockTerrain = tRockTerrain.getText().toString();
        String sLowBar = tLowBar.getText().toString();

        //Saves data
        sqLiteDatabase = myDB.getWritableDatabase();
        myDB.addInformation(sNumber, sPoticullis, sChevalFrise, sMoat, sRamparts, sDrawbridge, sSallyPort, sRockWall,
                sRockTerrain, sLowBar, sqLiteDatabase);
        Toast.makeText(getContext(), "Data Saved", Toast.LENGTH_LONG).show();
        myDB.close();
    } //End of addScoutInfo
} //End of class

DatabaseHelper.java

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "Scout.db";
    private static final int DATABASE_VERSION = 1;
    private static final String CREATE_QUERY = "CREATE TABLE " + DatabaseContract.NewDataInfo.TABLE_NAME + "(" + DatabaseContract.NewDataInfo.COL_NUMBER +
            " INTEGER PRIMARY KEY AUTOINCREMENT," + DatabaseContract.NewDataInfo.COL_POTICULLIS + " TEXT," + DatabaseContract.NewDataInfo.COL_CHEVAL_FRISE + " TEXT," +
            DatabaseContract.NewDataInfo.COL_MOAT + " TEXT," + DatabaseContract.NewDataInfo.COL_RAMPARTS + " TEXT," + DatabaseContract.NewDataInfo.COL_DRAWBRIDGE +
            " TEXT," + DatabaseContract.NewDataInfo.COL_SALLY_PORT + " TEXT," + DatabaseContract.NewDataInfo.COL_ROCK_WALL + " TEXT," +
            DatabaseContract.NewDataInfo.COL_ROCK_TERRAIN + " TEXT," + DatabaseContract.NewDataInfo.COL_LOW_BAR + "TEXT);";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.e("DATABASE OPERATIONS", "Database created / opened ...");
    } //End of DatabaseHelper

    @Override
    public void onCreate(SQLiteDatabase db) {
        //Create Query
        db.execSQL(CREATE_QUERY);

        //Display Log message
        Log.e("DATABASE OPERATIONS", "Table created...");
    } //End of onCreate

    public void addInformation(String eNumber, String ePoticullis, String eChevalFrise, String eMoat, String eRamparts, String eDrawbridge, String eSallyPort,
                               String eRockWall, String eRockTerrain, String eLowBar, SQLiteDatabase db) {
        //Instantiate contentValues
        ContentValues contentValues = new ContentValues();

        //Insert all content values
        contentValues.put(DatabaseContract.NewDataInfo.COL_NUMBER, eNumber);
        contentValues.put(DatabaseContract.NewDataInfo.COL_POTICULLIS, ePoticullis);
        contentValues.put(DatabaseContract.NewDataInfo.COL_CHEVAL_FRISE, eChevalFrise);
        contentValues.put(DatabaseContract.NewDataInfo.COL_MOAT, eMoat);
        contentValues.put(DatabaseContract.NewDataInfo.COL_RAMPARTS, eRamparts);
        contentValues.put(DatabaseContract.NewDataInfo.COL_DRAWBRIDGE, eDrawbridge);
        contentValues.put(DatabaseContract.NewDataInfo.COL_SALLY_PORT, eSallyPort);
        contentValues.put(DatabaseContract.NewDataInfo.COL_ROCK_WALL, eRockWall);
        contentValues.put(DatabaseContract.NewDataInfo.COL_ROCK_TERRAIN, eRockTerrain);
        contentValues.put(DatabaseContract.NewDataInfo.COL_LOW_BAR, eLowBar);

        //Insert content values into table
        db.insert(DatabaseContract.NewDataInfo.TABLE_NAME, null, contentValues);

        //Display log message
        Log.e("DATABASE OPERATIONS", "One row inserted...");
    } //End of addInformation

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    } //End of onUpgrade
} //End of class

DatabaseContract.java

public class DatabaseContract {
    public static abstract class NewDataInfo {
        public static final String TABLE_NAME = "scout_table";
        public static final String COL_NUMBER = "TEAM NUMBER";
        public static final String COL_POTICULLIS = "POTICULLIS";
        public static final String COL_CHEVAL_FRISE = "CHEVAL DE FRISE";
        public static final String COL_MOAT = "MOAT";
        public static final String COL_RAMPARTS = "RAMPARTS";
        public static final String COL_DRAWBRIDGE = "DRAWBRIDGE";
        public static final String COL_SALLY_PORT = "SALLY PORT";
        public static final String COL_ROCK_WALL = "ROCK WALL";
        public static final String COL_ROCK_TERRAIN = "ROCK TERRAIN";
        public static final String COL_LOW_BAR = "LOW BAR";
    } //End of NewDataInfo
} //End of class

记录错误

                                         Process: com.compscitutorials.basigarcia.ramfernoscout, PID: 1861
                                         android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1): , while compiling: CREATE TABLE scout_table(TEAM NUMBER INTEGER PRIMARY KEY AUTOINCREMENT,POTICULLIS TEXT,CHEVAL DE FRISE TEXT,MOAT TEXT,RAMPARTS TEXT,DRAWBRIDGE TEXT,SALLY PORT TEXT,ROCK WALL TEXT,ROCK TERRAIN TEXT,LOW BARTEXT);
                                             at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                             at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                             at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                             at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                             at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                             at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                             at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
                                             at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
                                             at com.compscitutorials.basigarcia.ramfernoscout.DatabaseHelper.onCreate(DatabaseHelper.java:26)
                                             at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                             at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                             at com.compscitutorials.basigarcia.ramfernoscout.AddScoutDataFragment.addScoutInfo(AddScoutDataFragment.java:96)
                                             at com.compscitutorials.basigarcia.ramfernoscout.AddScoutDataFragment$1.onClick(AddScoutDataFragment.java:55)
                                             at android.view.View.performClick(View.java:5198)
                                             at android.view.View$PerformClick.run(View.java:21147)
                                             at android.os.Handler.handleCallback(Handler.java:739)
                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                             at android.os.Looper.loop(Looper.java:148)
                                             at android.app.ActivityThread.main(ActivityThread.java:5417)
                                             at java.lang.reflect.Method.invoke(Native Method)
                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

冲突的名称DatabaseContract.java是导致此问题的原因。将名称更改为

public class DatabaseContract {
    public static abstract class NewDataInfo {
        public static final String TABLE_NAME = "scout_table";
        public static final String COL_NUMBER = "TEAM_NUMBER";
        public static final String COL_POTICULLIS = "POTICULLIS";
        public static final String COL_CHEVAL_FRISE = "CHEVAL_DE_FRISE";
        public static final String COL_MOAT = "MOAT";
        public static final String COL_RAMPARTS = "RAMPARTS";
        public static final String COL_DRAWBRIDGE = "DRAWBRIDGE";
        public static final String COL_SALLY_PORT = "SALLY_PORT";
        public static final String COL_ROCK_WALL = "ROCK_WALL";
        public static final String COL_ROCK_TERRAIN = "ROCK_TERRAIN";
        public static final String COL_LOW_BAR = "LOW_BAR";
    } //End of NewDataInfo
} //End of class

同样从代码中删除AutoIncrement并保持整数,因为编号无关紧要

DatabaseContract.NewDataInfo.COL_NUMBER + " INTEGER,"