我尝试从EditText
获取输入,点击button
后,它会将数据保存在数据库中并显示在textview
中。但是当我运行我的应用程序时,它会持续显示以下错误并显示白屏。
11-04 08:25:25.360 1925-1925/com.example.usaukglu.databaselearning I/art﹕ Not late-enabling -Xcheck:jni (already on)
11-04 08:25:26.329 1925-1937/com.example.usaukglu.databaselearning I/art﹕ WaitForGcToComplete blocked for 16.058ms for cause Background
11-04 08:25:26.875 1925-1937/com.example.usaukglu.databaselearning I/art﹕ Background sticky concurrent mark sweep GC freed 10318(8MB) AllocSpace objects, 0(0B) LOS objects, 0% free, 13MB/13MB, paused 60.083ms total 502.110ms
11-04 08:25:27.009 1925-1937/com.example.usaukglu.databaselearning W/art﹕ Suspending all threads took: 19.606ms
package com.example.usaukglu.databaselearning;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText editText;
Button button;
TextView textView;
MyDbHandler myDbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
myDbHandler=new MyDbHandler(this,null,null,1);
//printDatabase();
}
public void addButtonClicked(View view){
Product product =new Product(editText.getText().toString());
myDbHandler.addProduct(product);
printDatabase();
}
public void printDatabase() {
String dbString=myDbHandler.databaseToString();
textView.setText(dbString);
editText.setText("");
}
}
package com.example.usaukglu.databaselearning;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDbHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "product.db";
private static final String TABLE_NAME = "product";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_PRODUCTNAME = "productName";
public MyDbHandler(Context context, String name,SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query = "CREATE TABLE " + TABLE_NAME
+ "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_PRODUCTNAME +" TEXT);";
sqLiteDatabase.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(sqLiteDatabase);
}
public void addProduct(Product product) {
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.get_productName());
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
sqLiteDatabase.insert(TABLE_NAME, null, values);
sqLiteDatabase.close();
}
//Print out the database as a string
public String databaseToString() {
String dbString = "";
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
String query="SELECT * FROM " + TABLE_NAME + " WHERE 1";
//Cursor point to a location in results
Cursor c=sqLiteDatabase.rawQuery(query,null);
//Move to the first row in results
c.moveToFirst();
while (!c.isAfterLast()){
if (c.getString(c.getColumnIndex("productName"))!=null){
dbString+=c.getString(c.getColumnIndex("productName"));
dbString+="\n";
}
}
sqLiteDatabase.close();
return dbString;
}
}
package com.example.usaukglu.databaselearning;
public class Product {
private int _id;
private String _productName;
public Product(){
}
public Product(String productName){
this._productName = productName;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String get_productName() {
return _productName;
}
public void set_productName(String _productName) {
this._productName = _productName;
}
}