不幸的是申请已停止

时间:2014-12-03 17:50:24

标签: java android xml logcat android-logcat

我试图创建一个简单的Android SQLite数据库,在我尝试运行应用程序之后,它说"不幸的是,appName已停止"然后我检查我的LogCat,我遇到了一些错误。 这是我的错误日志:

12-03 12:23:37.659: E/SQLiteLog(1113): (1) near "tablehobiku": syntax error
12-03 12:23:37.689: E/AndroidRuntime(1113): FATAL EXCEPTION: main
12-03 12:23:37.689: E/AndroidRuntime(1113): Process: com.db.satu, PID: 1113
12-03 12:23:37.689: E/AndroidRuntime(1113): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.db.satu/com.db.satu.DatabaseAndroidSatu}: android.database.sqlite.SQLiteException: near "tablehobiku": syntax error (code 1): , while compiling: create tablehobiku (_id integer PRIMARY KEY autoincrement,nama text,hobi text)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.os.Looper.loop(Looper.java:136)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at java.lang.reflect.Method.invokeNative(Native Method)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at java.lang.reflect.Method.invoke(Method.java:515)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at dalvik.system.NativeStart.main(Native Method)
12-03 12:23:37.689: E/AndroidRuntime(1113): Caused by: android.database.sqlite.SQLiteException: near "tablehobiku": syntax error (code 1): , while compiling: create tablehobiku (_id integer PRIMARY KEY autoincrement,nama text,hobi text)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at com.db.satu.DatabaseManager$DatabaseOpenHelper.onCreate(DatabaseManager.java:45)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at com.db.satu.DatabaseManager.<init>(DatabaseManager.java:31)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at com.db.satu.DatabaseAndroidSatu.onCreate(DatabaseAndroidSatu.java:27)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.app.Activity.performCreate(Activity.java:5231)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-03 12:23:37.689: E/AndroidRuntime(1113):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-03 12:23:37.689: E/AndroidRuntime(1113):     ... 11 more

这是我的名为DatabaseAndroidSatu.java文件的类:

package com.db.satu;

    import java.util.ArrayList;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TextView;
    import android.widget.Toast;

    public class DatabaseAndroidSatu extends Activity {
    DatabaseManager dm;
    EditText nama, hobi;
    Button addBtn;
    TableLayout tabel4data;// tabel for data

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    dm = new DatabaseManager(this);
    tabel4data = (TableLayout) findViewById(R.id.tabel_data);
    nama = (EditText) findViewById(R.id.inNama);
    hobi = (EditText) findViewById(R.id.inHobi);
    addBtn = (Button) findViewById(R.id.btnAdd);
    addBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    simpKamuta();
    }
});
    updateTable();
    }

    protected void simpKamuta() {
    try {
    dm.addRow(nama.getText().toString(),hobi.getText().toString());
    Toast.makeText(getBaseContext(),
    nama.getText().toString() + ", berhasil disimpan",
    Toast.LENGTH_SHORT).show();
    updateTable();
    kosongkanField();
    } catch (Exception e) {
    e.printStackTrace();
    Toast.makeText(getBaseContext(), "gagal simpan, " +
        e.toString(),Toast.LENGTH_LONG).show();
    }
    }
    protected void kosongkanField(){
    nama.setText("");
    hobi.setText("");
    }
    protected void updateTable() {
    // TODO Auto-generated method stub
    while (tabel4data.getChildCount() > 1) {
    tabel4data.removeViewAt(1);
    }

    ArrayList<ArrayList<Object>> data = dm.ambilSemuaBaris();//

    for (int posisi = 0; posisi < data.size(); posisi++) {
    TableRow tabelBaris = new TableRow(this);
    ArrayList<Object> baris = data.get(posisi);

    TextView idTxt = new TextView(this);
    idTxt.setText(baris.get(0).toString());
    tabelBaris.addView(idTxt);

    TextView namaTxt = new TextView(this);
    namaTxt.setText(baris.get(1).toString());
    tabelBaris.addView(namaTxt);

    TextView hobiTxt = new TextView(this);
    hobiTxt.setText(baris.get(2).toString());
    tabelBaris.addView(hobiTxt);

    tabel4data.addView(tabelBaris);
    }
}}

这是另一个不是我创建的活动类的类,名为&#34; DatabaseManager.java&#34;:

package com.db.satu;

    import java.util.ArrayList;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;

    public class DatabaseManager {

    private static final String ROW_ID = "_id";
    private static final String ROW_NAMA = "nama";
    private static final String ROW_HOBI = "hobi";

    private static final String NAMA_DB = "DatabaseAndroidSatu";
    private static final String NAMA_TABEL = "hobiku";
    private static final int DB_VERSION = 1;

    private static final String CREATE_TABLE = "create table"+NAMA_TABEL+" ("+ROW_ID+" integer PRIMARY KEY autoincrement,"+ROW_NAMA+" text,"+ROW_HOBI+" text)";

    private final Context context;
    private DatabaseOpenHelper dbHelper;
    private SQLiteDatabase db;

        public DatabaseManager(Context ctx) {
        this.context = ctx;
    dbHelper = new DatabaseOpenHelper(context);
    db = dbHelper.getWritableDatabase();
    }

        private static class DatabaseOpenHelper extends
    SQLiteOpenHelper {

        public DatabaseOpenHelper(Context context) {
        super(context, NAMA_DB, null, DB_VERSION);
    // TODO Auto-generated constructor stub
    }

        @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(CREATE_TABLE);
    }

        @Override
    public void onUpgrade(SQLiteDatabase db, int oldVer, int
    newVer) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS "+NAMA_DB);
    onCreate(db);

    }
    }
    public void close() {
    dbHelper.close();
    }

    public void addRow(String nama, String hobi) {
    ContentValues values = new ContentValues();
    values.put(ROW_NAMA, nama);
    values.put(ROW_HOBI, hobi);
    try {
    db.insert(NAMA_TABEL, null, values);
    } catch (Exception e) {
    Log.e("DB ERROR", e.toString());
    e.printStackTrace();
    }
    }

    public ArrayList<ArrayList<Object>> ambilSemuaBaris() {
    ArrayList<ArrayList<Object>> dataArray = new
    ArrayList<ArrayList<Object>>();
    Cursor cur;
    try {
    cur = db.query(NAMA_TABEL,
    new String[] { ROW_ID, ROW_NAMA, ROW_HOBI }, null, null,
    null, null, null);
    cur.moveToFirst();
    if (!cur.isAfterLast()) {
    do {
        ArrayList<Object> dataList = new ArrayList<Object>();
        dataList.add(cur.getLong(0));
        dataList.add(cur.getString(1));
        dataList.add(cur.getString(2));
        dataArray.add(dataList);
        } while (cur.moveToNext());
        }
            } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        Log.e("DEBE ERROR", e.toString());
        }
        return dataArray;
        }
        }

这是我的main.xml脚本:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1">
    <EditText android:id="@+id/inNama"
        android:layout_height="wrap_content"
        android:layout_width="100dip"></EditText>
    <EditText android:id="@+id/inHobi"
        android:layout_height="wrap_content"
        android:layout_width="100dip"></EditText>
    <Button android:layout_width="wrap_content"
        android:id="@+id/btnAdd"
        android:layout_height="wrap_content"
        android:text="@string/btnAddtxt"></Button>
    </LinearLayout>
    <TableLayout android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/tabel_data">
    <TableRow android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <TextView android:layout_height="wrap_content"
        android:layout_width="50dip"
        android:text="@string/nomorLabel"
        android:id="@+id/no_id"></TextView>
    <TextView android:layout_height="wrap_content"
        android:layout_width="100dip"
        android:text="@string/namaLabel"
        android:id="@+id/nama_id"></TextView>
    <TextView android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:text="@string/hobiLabel"
        android:id="@+id/hobi_id"></TextView>
    </TableRow>
    </TableLayout>
</LinearLayout>

这是我的.xml文件,位于&#34;项目名称&#34; / res / menu /文件夹中:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.db.satu.DatabaseAndroidSatu" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>

</menu>

这是我的strings.xml:

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

<resources>
    <string name="hello">Masukkan nama dan hobi</string>
    <string name="app_name">Database Android #1</string>
    <string name="btnAddtxt">Add</string>
    <string name="namaLabel">Nama</string>
    <string name="hobiLabel">hobi</string>
    <string name="nomorLabel">No.</string>
    <string name="action_settings">Settings</string>
</resources>

2 个答案:

答案 0 :(得分:0)

您有拼写错误。使用table hobiku而非tablehobiku

答案 1 :(得分:0)

因为你有这个例外:

Caused by: android.database.sqlite.SQLiteException: near "tablehobiku": syntax error (code 1): , while compiling: create tablehobiku (_id integer PRIMARY KEY autoincrement,nama text,hobi text)

您的脚本不正确,必须是&#34;创建表格hobiku&#34;

所以你必须添加一个空格:

"create table"+NAMA_TABEL+"

一定是

"create table "+NAMA_TABEL+"