什么地方出了错?我的项目模拟器打开了应用程序但信息输入了应用程序崩溃

时间:2016-12-19 18:15:11

标签: java android

有人可以帮我理解日志中的信息吗?

D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
I/OpenGLRenderer: Initialized EGL, version 1.4
D/gralloc_ranchu: Emulator without host-side GPU emulation detected.
I/Choreographer: Skipped 42 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 62 frames!  The application may be doing too much work on its main thread.
E/SQLiteLog: (1) AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
D/AndroidRuntime: Shutting down VM
at com.rolandsexample.listadapter.DatabaseHelper.onCreate(DatabaseHelper.java:33)
  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
  at com.rolandsexample.listadapter.DatabaseHelper.addData(DatabaseHelper.java:45)
  at com.rolandsexample.listadapter.MainActivity.AddData(MainActivity.java:64)
  at com.rolandsexample.listadapter.MainActivity$2.onClick(MainActivity.java:51)
  at android.view.View.performClick(View.java:5198)

DatabaseHelper.java:

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "users.db";
    public static final String TABLE_NAME = "users_data";
    public static final String COL1 = "ID";
    public static final String COL2 =  "FIRSTNAME";
    public static final String COL3 =  "LASTNAME";
    public static final String COL4 = "FAVFOOD";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTERGER     PRIMARY KEY AUTOINCREMENT, " +
            " FIRSTNAME TEXT, LASTNAME TEXT, FAVFOOD TEXT)";
        db.execSQL(createTable);
    }

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

    public boolean addData(String fName, String lName, String fFood) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL2, fName);
        contentValues.put(COL3, lName);
        contentValues.put(COL4, fFood);

        long result = db.insert(TABLE_NAME, null, contentValues);

        // if date as inserted incorrectly it will return -1
        if (result == -1) {
            return false;
        }else {
            return true;
        }
    }

    // query for 1 week repeats
    public Cursor getListContents() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
           return data;
    }
}

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    EditText etFirstName, etLastName, etFavFood;
    Button btnAdd, btnView;
    DatabaseHelper myDB;


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

        etFavFood = (EditText) findViewById(R.id.etFavFood);
        etFirstName = (EditText) findViewById(R.id.etFirstName);
        etLastName = (EditText) findViewById(R.id.etLastName);
        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnView = (Button) findViewById(R.id.btnView);
        myDB = new DatabaseHelper(this);

        btnView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ViewListContents.class);
                startActivity(intent);
            }
        });

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String fName = etFirstName.getText().toString();
                String lName = etLastName.getText().toString();
                String fFood = etFavFood.getText().toString();
                if(fName.length() != 0 && lName.length() != 0 && fFood.length() != 0) {
                    AddData(fName, lName, fFood);
                    etFavFood.setText("");
                    etFirstName.setText("");
                    etLastName.setText("");
                } else {
                    Toast.makeText(MainActivity.this, "You must put something in the text field!", Toast.LENGTH_LONG).show();
                }
            }
        });

    }

    public void AddData(String firstName,String lastName, String favFood){
        boolean insertData=myDB.addData(firstName, lastName, favFood);

        if (insertData == true){
            Toast.makeText(MainActivity.this, "Data inserted successfully!", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(MainActivity.this, "Something went wrong :(.", Toast.LENGTH_LONG).show();
        }
    }


}

这可能是问题的两个方面?

1 个答案:

答案 0 :(得分:0)

在DatabaseHelper.java中的onCreate函数中:INTERGER应该是INTEGER