samedi 26 décembre 2015

When Insertdata, adddata and delete data the emulator couldn't open the application

MainActivity.java

android studio do not show any error in this code, but maybe it still have some problem.

package com.example.brian.ch15_myhotline;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity implements AdapterView.OnItemClickListener {

static final String DB_NAME="HotlineDB";
static final String TB_NAME="hotlist";
static final  int MAX=8;
static final String[] FROM = new String[] {"name" , "phone" , "list"};
SQLiteDatabase db;
Cursor cur;
SimpleCursorAdapter adapter;
EditText etName, etPhone , etList;
Button btInsert , btUpdate , btDelete;
ListView lv;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    etName=(EditText)findViewById(R.id.etName);
    etPhone=(EditText)findViewById(R.id.etPhone);
    etList=(EditText)findViewById(R.id.etList);
    btInsert=(Button)findViewById(R.id.btInsert);
    btUpdate=(Button)findViewById(R.id.btUpdate);
    btDelete=(Button)findViewById(R.id.btDelete);
    db=openOrCreateDatabase(DB_NAME , Context.MODE_PRIVATE, null);
    String createTable= "CREATE TABLE IF NOT EXISTS " + TB_NAME + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "name VARCHAR(32), " +
            "phone VARCHAR(16) " +
            "list VARCHAR(100))";
    db.execSQL(createTable);
    cur=db.rawQuery("SELECT * FROM "+ TB_NAME, null);
    if (cur.getCount()==0) {
        addData("姓名" , "0932828387", "點餐資訊");
    }
    adapter=new SimpleCursorAdapter(this,R.layout.item, cur, FROM, new int[] {R.id.name, R.id.phone, R.id.list}, 0);
    lv=(ListView)findViewById(R.id.lv);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(this);
    requery();
}

private void addData(String name, String phone, String list) {
    ContentValues cv=new ContentValues(3);
    cv.put(FROM[0], name);
    cv.put(FROM[1], phone);
    cv.put(FROM[2], list);

    db.insert(TB_NAME, null, cv);
}

private void update(String name, String phone, String list, int id) {
    ContentValues cv=new ContentValues(3);
    cv.put(FROM[0], name);
    cv.put(FROM[1], phone);
    cv.put(FROM[2], list);

    db.update(TB_NAME, cv, "_id=" + id, null);
}

private void requery() {
    cur=db.rawQuery("SELECT * FROM "+TB_NAME, null);
    adapter.changeCursor(cur);
    if (cur.getCount()==MAX)
        btInsert.setEnabled(false);
    else
        btInsert.setEnabled(true);
    btUpdate.setEnabled(false);
    btDelete.setEnabled(false);
}

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    cur.moveToPosition(position);

    etName.setText(cur.getString(cur.getColumnIndex(FROM[0])));
    etPhone.setText(cur.getString(cur.getColumnIndex(FROM[1])));
    etList.setText(cur.getString(cur.getColumnIndex(FROM[2])));

    btUpdate.setEnabled(true);
    btDelete.setEnabled(true);
}

public void onInsertUpdate(View v) {
    String nameStr=etName.getText().toString().trim();
    String phoneStr=etPhone.getText().toString().trim();
    String listStr=etList.getText().toString().trim();
    if (nameStr.length()==0 || phoneStr.length()==0 || listStr.length()==0) return;

    if (v.getId()==R.id.btUpdate)
        update(nameStr, phoneStr, listStr, cur.getInt(0));
    else
        addData(nameStr, phoneStr, listStr);
    requery();
}
        public void onDelete(View v) {
            db.delete(TB_NAME, "_id="+cur.getInt(0), null);
        requery();
        }




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


}

activity_main.xml

 <LinearLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:orientation="vertical"
    android:weightSum="1">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/lv"
        android:layout_weight="0.61" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.11">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="姓名:"
            android:id="@+id/textView" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/etName"
            android:layout_weight="1" />

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="31dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.04">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="電話:"
            android:id="@+id/textView2" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/etPhone"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="點餐資料:"
            android:id="@+id/textView3" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/etList"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:layout_weight="0.02">

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="新增"
            android:id="@+id/btInsert"
            android:onClick="onInsertUpdate" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="修改"
            android:id="@+id/btUpdate"
            android:onClick="onInsertUpdate" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="刪除"
            android:id="@+id/btDelete"
            android:onClick="onDelete" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button4" />
    </LinearLayout>

</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:weightSum="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:text="New Text"
        android:id="@+id/name"
        android:layout_weight="0.10"
        android:textSize="30dp"
        android:textColor="#07a"
        android:layout_marginRight="5dp" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="232dp"
        android:layout_height="48dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/phone"
            android:textColor="#840"
            android:textSize="14dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/list"
            android:textSize="14dp"
            android:textColor="#480" />
    </LinearLayout>
</LinearLayout>

logcat

12-26 04:42:17.897      595-622/? I/ActivityManager﹕ Waited long enough for: ServiceRecord{34fea538 u0 com.android.music/.MediaPlaybackService}
    --------- beginning of main
12-26 04:42:23.800      595-672/? E/WifiStateMachine﹕ CMD_START_SCAN : connected mode and no configuration
12-26 04:42:26.636      595-622/? I/ActivityManager﹕ Waited long enough for: ServiceRecord{3de74a14 u0 com.android.calendar/.alerts.InitAlarmsService}
12-26 04:42:29.201    1773-1773/? D/AndroidRuntime﹕ >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<<
12-26 04:42:29.202    1773-1773/? D/AndroidRuntime﹕ CheckJNI is OFF
12-26 04:42:29.227    1773-1773/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
12-26 04:42:29.227    1773-1773/? E/android.os.Debug﹕ failed to load memtrack module: -2
12-26 04:42:29.240    1773-1773/? D/AndroidRuntime﹕ Calling main entry com.android.commands.am.Am
12-26 04:42:29.243     595-1075/? I/ActivityManager﹕ Force stopping com.example.brian.ch15_myhotline appid=10062 user=0: from pid 1773
12-26 04:42:29.244    1773-1773/? D/AndroidRuntime﹕ Shutting down VM
12-26 04:42:29.550    1789-1789/? D/AndroidRuntime﹕ >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<<
12-26 04:42:29.552    1789-1789/? D/AndroidRuntime﹕ CheckJNI is OFF
12-26 04:42:29.576    1789-1789/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
12-26 04:42:29.576    1789-1789/? E/android.os.Debug﹕ failed to load memtrack module: -2
12-26 04:42:29.588    1789-1789/? D/AndroidRuntime﹕ Calling main entry com.android.commands.am.Am
12-26 04:42:29.594      595-954/? I/ActivityManager﹕ START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.brian.ch15_myhotline/.MainActivity} from uid 0 on display 0
12-26 04:42:29.594      595-954/? V/WindowManager﹕ addAppToken: AppWindowToken{2df54b5e token=Token{255bac99 ActivityRecord{19a431e0 u0 com.example.brian.ch15_myhotline/.MainActivity t14}}} to stack=1 task=14 at 0
12-26 04:42:29.602    1789-1789/? D/AndroidRuntime﹕ Shutting down VM
12-26 04:42:29.607      595-633/? V/WindowManager﹕ Adding window Window{b1450a4 u0 Starting com.example.brian.ch15_myhotline} at 2 of 7 (after Window{28411ca3 u0 com.android.launcher3/com.android.launcher3.Launcher})
12-26 04:42:29.614    1802-1802/? I/art﹕ Late-enabling -Xcheck:jni
12-26 04:42:29.621      595-829/? I/ActivityManager﹕ Start proc 1802:com.example.brian.ch15_myhotline/u0a62 for activity com.example.brian.ch15_myhotline/.MainActivity
12-26 04:42:29.752    1802-1802/? E/SQLiteLog﹕ (1) near "list": syntax error
12-26 04:42:29.752    1802-1802/? D/AndroidRuntime﹕ Shutting down VM
    --------- beginning of crash
12-26 04:42:29.753    1802-1802/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.brian.ch15_myhotline, PID: 1802
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brian.ch15_myhotline/com.example.brian.ch15_myhotline.MainActivity}: android.database.sqlite.SQLiteException: near "list": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS hotlist(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(32), phone VARCHAR(16) list VARCHAR(100))
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: android.database.sqlite.SQLiteException: near "list": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS hotlist(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(32), phone VARCHAR(16) list VARCHAR(100))
            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:1674)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
            at com.example.brian.ch15_myhotline.MainActivity.onCreate(MainActivity.java:48)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
12-26 04:42:29.757      595-682/? W/ActivityManager﹕ Force finishing activity 1 com.example.brian.ch15_myhotline/.MainActivity
12-26 04:42:29.760      613-616/? D/PermissionCache﹕ checking android.permission.READ_FRAME_BUFFER for uid=1000 => granted (220 us)
12-26 04:42:29.788      613-613/? E/EGL_emulation﹕ tid 613: eglCreateSyncKHR(1209): error 0x3004 (EGL_BAD_ATTRIBUTE)
12-26 04:42:29.948     595-1426/? I/OpenGLRenderer﹕ Initialized EGL, version 1.4
12-26 04:42:29.985     595-1426/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented
12-26 04:42:29.985     595-1426/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xe14cdbc0, error=EGL_SUCCESS
12-26 04:42:30.190     595-1426/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented
12-26 04:42:30.190     595-1426/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xe14cdbc0, error=EGL_SUCCESS
12-26 04:42:30.652      595-622/? W/ActivityManager﹕ Activity pause timeout for ActivityRecord{19a431e0 u0 com.example.brian.ch15_myhotline/.MainActivity t14 f}
12-26 04:42:30.722     926-1132/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented
12-26 04:42:30.722     926-1132/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xeed44a20, error=EGL_SUCCESS
12-26 04:42:30.837      595-954/? I/ActivityManager﹕ Killing 1584:com.svox.pico/u0a49 (adj 15): empty #17
12-26 04:42:31.368     926-1132/? W/OpenGLRenderer﹕ Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
12-26 04:42:31.666      716-716/? W/ResourceType﹕ No package identifier when getting value for resource number 0x00000000
12-26 04:42:31.666      716-716/? W/PackageManager﹕ Failure retrieving resources for com.example.brian.ch15_myhotline: Resource ID #0x0
12-26 04:42:40.874      595-622/? W/ActivityManager﹕ Activity destroy timeout for ActivityRecord{19a431e0 u0 com.example.brian.ch15_myhotline/.MainActivity t14 f}
12-26 04:42:41.637    1367-1703/? D/InitAlarmsService﹕ Clearing and rescheduling alarms.
12-26 04:42:41.673      595-991/? I/ActivityManager﹕ Killing 993:android.process.acore/u0a2 (adj 15): empty #17
12-26 04:42:43.814      595-672/? E/WifiStateMachine﹕ CMD_START_SCAN : connected mode and no configuration
12-26 04:42:44.380      595-695/? D/TaskPersister﹕ removeObsoleteFile: deleting file=14_task.xml
12-26 04:42:44.380      595-695/? D/TaskPersister﹕ removeObsoleteFile: deleting file=14_task_thumbnail.png
12-26 04:43:03.817      595-672/? E/WifiStateMachine﹕ CMD_START_SCAN : connected mode and no configuration
12-26 04:43:05.127      595-954/? I/MediaFocusControl﹕ AudioFocus  abandonAudioFocus() from android.media.AudioManager@1b9660bccom.android.music.MediaPlaybackService$3@1de8a845
12-26 04:43:05.128    1485-1485/? V/MusicFXControlPanelReceiver﹕ onReceive
12-26 04:43:05.128    1485-1485/? V/MusicFXControlPanelReceiver﹕ Action: android.media.action.CLOSE_AUDIO_EFFECT_CONTROL_SESSION
12-26 04:43:05.128    1485-1485/? V/MusicFXControlPanelReceiver﹕ Package name: com.android.music
12-26 04:43:05.128    1485-1485/? V/MusicFXControlPanelReceiver﹕ Audio session: 8
12-26 04:43:05.128    1485-1485/? V/MusicFXControlPanelEffect﹕ closeSession(android.app.ReceiverRestrictedContext@90c88c0, com.android.music, 8)
12-26 04:43:05.130      595-947/? I/ActivityManager﹕ Killing 1183:com.android.music/u0a42 (adj 15): empty #17
12-26 04:43:05.132      209-209/? E/lowmemorykiller﹕ Error writing /proc/1183/oom_score_adj; errno=22
12-26 04:43:10.150      595-679/? W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client
12-26 04:43:10.163    1802-1802/? I/Process﹕ Sending signal. PID: 1802 SIG: 9
12-26 04:43:10.185      595-954/? I/ActivityManager﹕ Process com.example.brian.ch15_myhotline (pid 1802) has died
12-26 04:43:10.217     595-1426/? D/OpenGLRenderer﹕ endAllStagingAnimators on 0xda83df80 (RippleDrawable) with handle 0xda8ff9e0
12-26 04:43:10.221      595-829/? W/InputMethodManagerService﹕ Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@10556917 attribute=null, token = android.os.BinderProxy@1f3c93d2
12-26 04:43:23.827      595-672/? E/WifiStateMachine﹕ CMD_START_SCAN : connected mode and no configuration

It seems like did not have any error in my code but I sill can't open it. Hope someone can help me solve this problem thank you so much.

Aucun commentaire:

Enregistrer un commentaire