错误:不幸的是App停止了

时间:2016-02-08 13:25:55

标签: android

我正在尝试通过SQLITE数据库创建一个帐户表单,但是当我尝试在手机或模拟器上运行时,它显示“不幸的是APP已停止”。

这是我的Mnifest文件代码

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这是我的MainActivity

public class MainActivity extends AppCompatActivity {

DatabaseHelper myDb;

EditText editName, editPassword, editEmail;

Button buttonRegister;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myDb=new DatabaseHelper(this);

    editName = (EditText) findViewById(R.id.edit_name);
    editEmail = (EditText) findViewById(R.id.edit_email);

    editPassword = (EditText) findViewById(R.id.edit_password);

    buttonRegister = (Button) findViewById(R.id.ButtonRegister);

    Reg();
}

public void Reg(){
    buttonRegister.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isInserted=myDb.insertData(editName.getText().toString(),
                            editEmail.getText().toString(),
                            editPassword.getText().toString());
                    if(isInserted==true)
                        Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_SHORT).show();

                }
            }
    );
}

}

这是我的DatabaseHelper类

public class DatabaseHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="USER.db";
private static final String TABLE_NAME="USER";
private static final String COL_1="ID";
private static final String COL_2="NAME";
private static final String COL_3="EMAIL";
private static final String COL_4="PASS";
SQLiteDatabase db;



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


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, EMAIL TEXT, PASS TEXT)");

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

}
public boolean insertData(String name, String email, String pass)
{
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues values=new ContentValues();
    ContentValues contentValues=new ContentValues();
    contentValues.put(COL_2,name);
    contentValues.put(COL_3,email);
    contentValues.put(COL_4,pass);
    long result=db.insert(TABLE_NAME, null, contentValues);
    if (result==-1)
        return false;
    else
        return true;
}

}

这是我的XML文件

<?xml version="1.0" encoding="utf-8"?>

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Name"
    android:ems="10"
    android:id="@+id/edit_name"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:id="@+id/edit_password"
    android:hint="Password"
    android:layout_below="@+id/edit_email"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:id="@+id/edit_email"
    android:layout_below="@+id/edit_name"
    android:layout_alignParentStart="true"
    android:hint="Email" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Register"
    android:id="@+id/ButtonRegister"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

这是我的LogCat

02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime: FATAL EXCEPTION: main
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime: Process: com.example.mubbasher.howdy, PID: 6442
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime: android.database.sqlite.SQLiteException: near "tableUSER": syntax error (code 1): , while compiling: create tableUSER(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, EMAIL TEXT, PASS TEXT)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1806)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1737)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at com.example.mubbasher.howdy.DatabaseHelper.onCreate(DatabaseHelper.java:32)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at com.example.mubbasher.howdy.DatabaseHelper.insertData(DatabaseHelper.java:43)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at com.example.mubbasher.howdy.MainActivity$1.onClick(MainActivity.java:35)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.view.View.performClick(View.java:4661)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19498)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:146)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5641)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
02-08 18:42:32.685 6442-6442/com.example.mubbasher.howdy I/Process: Sending signal. PID: 6442 SIG: 9

4 个答案:

答案 0 :(得分:1)

更改为这样(添加空格)

public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, EMAIL TEXT, PASS TEXT)");

}

也不要忘记解决这个问题,(在存在后添加空格)

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

}

答案 1 :(得分:0)

在DatabaseHelper onCreate()中,您缺少2个空格(表名前后):

@Inject
EPartService partService;

...

partService.showPart("part id", PartState.ACTIVATE);

答案 2 :(得分:0)

您应该在表名之前和之后添加空格,例如"create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, EMAIL TEXT, PASS TEXT)"

答案 3 :(得分:0)

忘记在表名之前和之后添加空格。

更改

 db.execSQL("create table" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, EMAIL TEXT, PASS TEXT)");

 db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, EMAIL TEXT, PASS TEXT)");