我的日志是:
09-04 09:04:39.997 18653-18653/com.example.yahya.sqlitesample D/dalvikvm﹕ Late-enabling CheckJNI
09-04 09:04:40.185 18653-18653/com.example.yahya.sqlitesample I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
09-04 09:04:40.185 18653-18653/com.example.yahya.sqlitesample W/dalvikvm﹕ VFY: unable to resolve virtual method 410: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
09-04 09:04:40.185 18653-18653/com.example.yahya.sqlitesample D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
09-04 09:04:40.185 18653-18653/com.example.yahya.sqlitesample I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
09-04 09:04:40.189 18653-18653/com.example.yahya.sqlitesample W/dalvikvm﹕ VFY: unable to resolve virtual method 432: Landroid/content/res/TypedArray;.getType (I)I
09-04 09:04:40.189 18653-18653/com.example.yahya.sqlitesample D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
09-04 09:04:40.237 18653-18653/com.example.yahya.sqlitesample I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
09-04 09:04:40.237 18653-18653/com.example.yahya.sqlitesample W/dalvikvm﹕ VFY: unable to resolve virtual method 373: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
09-04 09:04:40.237 18653-18653/com.example.yahya.sqlitesample D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
09-04 09:04:40.237 18653-18653/com.example.yahya.sqlitesample I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
09-04 09:04:40.237 18653-18653/com.example.yahya.sqlitesample W/dalvikvm﹕ VFY: unable to resolve virtual method 375: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
09-04 09:04:40.237 18653-18653/com.example.yahya.sqlitesample D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
09-04 09:04:40.425 18653-18653/com.example.yahya.sqlitesample D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
09-04 09:04:40.425 18653-18653/com.example.yahya.sqlitesample D/﹕ HostConnection::get() New Host Connection established 0xb938abe0, tid 18653
09-04 09:04:40.465 18653-18653/com.example.yahya.sqlitesample D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
09-04 09:04:40.465 18653-18653/com.example.yahya.sqlitesample D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
09-04 09:04:40.665 18653-18653/com.example.yahya.sqlitesample W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
09-04 09:04:40.701 18653-18653/com.example.yahya.sqlitesample E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
09-04 09:04:40.713 18653-18653/com.example.yahya.sqlitesample E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
09-04 09:04:40.757 18653-18653/com.example.yahya.sqlitesample E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
09-04 09:04:40.769 18653-18653/com.example.yahya.sqlitesample E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
09-04 09:04:40.769 18653-18653/com.example.yahya.sqlitesample D/OpenGLRenderer﹕ Enabling debug mode 0
09-04 09:04:46.777 18653-18653/com.example.yahya.sqlitesample E/SQLiteLog﹕ (1) near "productname": syntax error
09-04 09:04:46.781 18653-18653/com.example.yahya.sqlitesample D/AndroidRuntime﹕ Shutting down VM
09-04 09:04:46.781 18653-18653/com.example.yahya.sqlitesample W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4d5fb20)
09-04 09:04:46.789 18653-18653/com.example.yahya.sqlitesample E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.yahya.sqlitesample, PID: 18653
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "productname": syntax error (code 1): , while compiling: CREATE TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT productname TEXT );
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
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:1672)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
at com.example.yahya.sqlitesample.MyDBHandler.onCreate(MyDBHandler.java:34)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.example.yahya.sqlitesample.MyDBHandler.addProduct(MyDBHandler.java:49)
at com.example.yahya.sqlitesample.MainActivity.addButtonClicked(MainActivity.java:33)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
09-04 09:04:46.797 18653-18653/com.example.yahya.sqlitesample D/dalvikvm﹕ GC_FOR_ALLOC freed 240K, 9% free 3282K/3576K, paused 5ms, total 6ms
09-04 09:04:50.669 18653-18653/com.example.yahya.sqlitesample I/Process﹕ Sending signal. PID: 18653 SIG: 9
MyDBHandler:
package com.example.yahya.sqlitesample;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
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 = "products.db";
public static final String TABLE_PRODUCTS = "products";
public static final String COLUMN_ID = "_id";
public 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 db)
{
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
COLUMN_PRODUCTNAME + " TEXT " +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
//Add a new item to the db
public void addProduct(Products product)
{
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.get_productname());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_PRODUCTS, null, values);
db.close();
}
//Delete a product from db
public void deleteProduct(String name)
{
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + name + "\";");
}
//Print out the database as String
public String dbToString()
{
String dbString = new String();
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast())
{
if(c.getString(c.getColumnIndex("productname"))!=null)
{
dbString += c.getString((c.getColumnIndex("productname")));
dbString += "\n";
}
}
db.close();
return dbString;
}
}
主要活动类是:
package com.example.yahya.sqlitesample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity
{
EditText yTextInout;
TextView yTextView;
MyDBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yTextInout = (EditText) findViewById(R.id.yText);
yTextView = (TextView) findViewById(R.id.ytextView);
dbHandler = new MyDBHandler(this,null, null, 1);
printDataBase();
}
public void addButtonClicked(View view)
{
Products product = new Products(yTextInout.toString());
dbHandler.addProduct(product);
printDataBase();
}
public void deleteButtonClicked(View view)
{
String productName = yTextInout.getText().toString();
dbHandler.deleteProduct(productName);
// printDataBase();
}
public void printDataBase()
{
String dbString = dbHandler.dbToString();
yTextView.setText(dbString);
yTextInout.setText("");
}
}
产品类:
package com.example.yahya.sqlitesample;
/**
* Created by Yahya on 9/3/2015.
*/
public class Products
{
private int _id;
private String _productname;
public Products()
{
}
public Products(String productName)
{
this._productname = productName;
}
public void set_id(int _id)
{
this._id = _id;
}
public void set_productname(String _productName)
{
this._productname = _productName;
}
public int get_id()
{
return _id;
}
public String get_productname()
{
return _productname;
}
}
和activity_main.xml:
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/yText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="75dp"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/addButton"
android:layout_below="@+id/yText"
android:layout_alignLeft="@+id/yText"
android:layout_alignStart="@+id/yText"
android:layout_marginTop="30dp"
android:onClick="addButtonClicked" />
<Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Delete"
android:id="@+id/deleteButton"
android:layout_alignBottom="@+id/addButton"
android:layout_alignRight="@+id/yText"
android:layout_alignEnd="@+id/yText"
android:onClick="deleteButtonClicked" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/ytextView"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/addButton"
android:layout_toEndOf="@+id/addButton" />
当我想运行我的代码时,它会产生异常并退出程序,不幸的是hass已被停止。 请帮我修复例外情况。
答案 0 :(得分:2)
检查此行 引起:android.database.sqlite.SQLiteException:near&#34; productname&#34 ;:语法错误(代码1):,同时编译:CREATE TABLE产品(_id INTEGER PRIMARY KEY AUTOINCREMENT productname TEXT);
在第一列名称和检查
之后,您在上面只有put语法错误String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT " +
");";
答案 1 :(得分:1)
好的,最后我解决了我的问题,那里有3件事:
1-在MyDBHandler类中,应该在列的名称后面插入一个逗号,如Pavan建议的那样:
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT " +
");";
2-第二个问题出现在我的addButtonClicked函数中(在主要活动中)我错误地使用了以下内容:
yTextInput.toString();
我不得不使用以下内容:
yTextInput.getText().toString();
3-最后是dbToString函数中最重要的一个(MyDBHandler类),我没有在while中移动光标,所以while持续了。我为此目的使用了c.moveToNext。
while(!c.isAfterLast())
{
if(c.getString(c.getColumnIndex("productname"))!=null)
{
dbString += c.getString(c.getColumnIndex("productname"));
dbString += "\n";
}
c.moveToNext();
}
实际上,我使用New Boston教程来改进我的android编码。非常感谢Bucky Robert提供的精彩教程,但它有时会在教程编码中出现这样的错误,导致颈部疼痛! (并花了3天时间来解决它)。