该应用程序崩溃说"不幸的是应用已停止"当我在模拟器中运行时。并显示错误AndroidRuntime:FATAL EXCEPTION:main java.lang.RuntimeException:无法在我的logcat中启动活动ComponentInfo。请帮我解决这个错误,因为我是java和android的新手。 MainActivity.java
package com.example.kiran.lamolamo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText Input;
TextView Text;
MyDBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Input = (EditText)findViewById(R.id.buckysInput);
Text = (TextView) findViewById(R.id.buckysText);
dbHandler= new MyDBHandler(this, null, null, 1);
printDatabase();
}
public void addButtonClicked(View view){
Products product = new Products(Input.getText().toString());
dbHandler.addproduct(product);
printDatabase();
}
public void deleteButtonClicked(View view) {
String inputText = Input.getText().toString();
dbHandler.deleteproduct(inputText);
printDatabase();
}
public void printDatabase() {
String dbString = dbHandler.databaseToString();
Text.setText(dbString);
Input.setText("");
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.kiran.lamolamo.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:id="@+id/buckysInput" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/addUser"
android:onClick="addButtonClicked"
android:text="Add User"
android:layout_marginLeft="21dp"
android:layout_marginStart="21dp"
android:layout_marginTop="40dp"
android:layout_below="@+id/buckysInput"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<Button
android:text="Button"
android:onClick="deleteButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/addUser"
android:layout_alignRight="@+id/buckysInput"
android:layout_alignEnd="@+id/buckysInput"
android:id="@+id/button" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="152dp"
android:id="@+id/buckysText"
android:textAppearance="@style/TextAppearance.AppCompat.Display2" />
</RelativeLayout>
Products.java
package com.example.kiran.lamolamo;
public class Products {
private int _id;
private String _productname;
public Products(){};
public Products(String productname) {
this.set_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;
}
}
MyDBHandler.java
package com.example.kiran.lamolamo;
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_VESION = 1;
private static final String DATABASE_NAME = "products.db";
public static final String TABLE_NAME = "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_VESION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String Query = "CREATE TABLE" + TABLE_NAME + "(" +
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_NAME);
onCreate(db);
}
public void addproduct(Products product) {
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME,product.get_productname());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NAME,null,values);
db.close();
}
public void deleteproduct(String productName) {
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM" + TABLE_NAME + "WHERE"+COLUMN_PRODUCTNAME + "=\"" + productName + "\";");
}
public String databaseToString() {
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String Query = "SELECT * FROM " + TABLE_NAME + "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;
}
}
logcat的
11-11 18:53:20.979 30992-30992/? D/dalvikvm: Late-enabling CheckJNI
11-11 18:53:21.063 30992-30992/com.example.kiran.lamolamo I/dalvikvm: Could not find method android.app.Application.registerOnProvideAssistDataListener, referenced from method com.android.tools.fd.runtime.BootstrapApplication.registerOnProvideAssistDataListener
11-11 18:53:21.067 30992-30992/com.example.kiran.lamolamo W/dalvikvm: VFY: unable to resolve virtual method 242: Landroid/app/Application;.registerOnProvideAssistDataListener (Landroid/app/Application$OnProvideAssistDataListener;)V
11-11 18:53:21.067 30992-30992/com.example.kiran.lamolamo D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-11 18:53:21.143 30992-30992/com.example.kiran.lamolamo I/dalvikvm: Could not find method android.app.Application.unregisterOnProvideAssistDataListener, referenced from method com.android.tools.fd.runtime.BootstrapApplication.unregisterOnProvideAssistDataListener
11-11 18:53:21.143 30992-30992/com.example.kiran.lamolamo W/dalvikvm: VFY: unable to resolve virtual method 245: Landroid/app/Application;.unregisterOnProvideAssistDataListener (Landroid/app/Application$OnProvideAssistDataListener;)V
11-11 18:53:21.143 30992-30992/com.example.kiran.lamolamo D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-11 18:53:21.143 30992-30992/com.example.kiran.lamolamo I/InstantRun: Instant Run Runtime started. Android package is com.example.kiran.lamolamo, real application class is null.
11-11 18:53:21.175 30992-30992/com.example.kiran.lamolamo W/InstantRun: No instant run dex files added to classpath
11-11 18:53:21.179 30992-30992/com.example.kiran.lamolamo E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
11-11 18:53:21.183 30992-30992/com.example.kiran.lamolamo W/dalvikvm: VFY: unable to resolve check-cast 1704 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher;
11-11 18:53:21.183 30992-30992/com.example.kiran.lamolamo D/dalvikvm: VFY: replacing opcode 0x1f at 0x025e
11-11 18:53:21.183 30992-30992/com.example.kiran.lamolamo E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
11-11 18:53:21.183 30992-30992/com.example.kiran.lamolamo W/dalvikvm: VFY: unable to resolve const-class 1704 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher;
11-11 18:53:21.183 30992-30992/com.example.kiran.lamolamo D/dalvikvm: VFY: replacing opcode 0x1c at 0x0060
11-11 18:53:21.295 30992-30992/com.example.kiran.lamolamo E/SQLiteLog: (1) near "TABLEproducts": syntax error
11-11 18:53:21.295 30992-30992/com.example.kiran.lamolamo D/AndroidRuntime: Shutting down VM
11-11 18:53:21.303 30992-30992/com.example.kiran.lamolamo W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa62c4288)
11-11 18:53:21.303 30992-30992/com.example.kiran.lamolamo E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kiran.lamolamo/com.example.kiran.lamolamo.MainActivity}: android.database.sqlite.SQLiteException: near "TABLEproducts": syntax error (code 1): , while compiling: CREATE TABLEproducts(_idINTEGER PRIMARY KEY AUTOINCREMENTproductnameTEXT);
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "TABLEproducts": syntax error (code 1): , while compiling: CREATE TABLEproducts(_idINTEGER PRIMARY KEY AUTOINCREMENTproductnameTEXT);
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
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:1663)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
at com.example.kiran.lamolamo.MyDBHandler.onCreate(MyDBHandler.java:31)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.example.kiran.lamolamo.MyDBHandler.databaseToString(MyDBHandler.java:55)
at com.example.kiran.lamolamo.MainActivity.printDatabase(MainActivity.java:42)
at com.example.kiran.lamolamo.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
崩溃原因是错误的,在MyDBHandler.java
中你忘记了5个空格和1个逗号。
一定是
public class MyDBHandler extends SQLiteOpenHelper{
...
@Override
public void onCreate(SQLiteDatabase db) {
String Query = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT" +
");";
db.execSQL(Query);
}
...
答案 1 :(得分:0)
你真正的问题
引起:android.database.sqlite.SQLiteException:靠近“TABLEproducts”:语法错误(代码1):,同时编译:CREATE TABLEproducts(_idINTEGER PRIMARY KEY AUTOINCREMENTproductnameTEXT);
原因
String Query = "CREATE TABLE" + TABLE_NAME + "(" +
COLUMN_ID + "INTEGER PRIMARY KEY AUTOINCREMENT" +
COLUMN_PRODUCTNAME + "TEXT" +
");";
解决方案
String Query = "CREATE TABLE " + TABLE_NAME + "(" +
^ add a space
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
^ add a space ^^ add a comma, and space
COLUMN_PRODUCTNAME + " TEXT" +
^ add a space
");";
我喜欢在查询执行之前调试日志。
if ( debugQuery ) {
Log.d("MyDBHelper", "Executing query [ " + query + " ]");
}
db.execSQL(Query);
似乎在调用onUpgrade
时也会遇到同样的问题。
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
再次,错过一个空格
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
^ here
再次询问......
String Query = "SELECT * FROM " + TABLE_NAME + "WHERE 1";
应该是
String Query = "SELECT * FROM " + TABLE_NAME + " WHERE 1";
^ here