2014-02-27 158 views
0

模擬器不響應此代碼。「不幸的是,app_name已停止工作」

它給人的錯誤爲「Unfortunatey,法案已停止」。 我只用一個字段作爲學生的名字,嘗試了相同的代碼,它運行良好,但有兩個字段作爲學生的名字和位置沒有響應。 請幫助我。

MainActivity.java

package com.act; 

      import java.util.List; 

      import android.app.ListActivity; 
      import android.os.Bundle; 
      import android.view.View; 
     import android.widget.ArrayAdapter; 
      import android.widget.EditText; 
      public class MainActivity extends ListActivity { 

       private StudentOperations studentDBoperation; 

       @Override 
       public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
         studentDBoperation = new StudentOperations(this); 
        studentDBoperation.open(); 

        List values = studentDBoperation.getAllStudents(); 

        // Use the SimpleCursorAdapter to show the 
        // elements in a ListView 
        ArrayAdapter adapter = new ArrayAdapter(this, 
       android.R.layout.simple_list_item_1, values); 
        setListAdapter(adapter); 
       } 

       public void addUser(View view) { 
         ArrayAdapter adapter = (ArrayAdapter) getListAdapter(); 
         EditText text = (EditText) findViewById(R.id.editText1); 
         EditText text2 = (EditText) findViewById(R.id.editText2); 

        Student stud = studentDBoperation.addStudent(text.getText().toString(),text2.getText().toString()); 
         adapter.add(stud); 

       } 
        public void deleteFirstUser(View view) { 
         ArrayAdapter adapter = (ArrayAdapter) getListAdapter(); 
        Student stud = null; 
         if (getListAdapter().getCount() > 0) { 
         stud = (Student) getListAdapter().getItem(0); 
         studentDBoperation.deleteStudent(stud); 
         adapter.remove(stud); 
        } 
        } 

       @Override 
       protected void onResume() { 
        studentDBoperation.open(); 
        super.onResume(); 
       } 
       @Override 
       protected void onPause() { 
         studentDBoperation.close(); 
         super.onPause(); 
       } 

         } 

DataBaseWrapper.java

package com.act; 

       import android.content.Context; 
      import android.database.sqlite.SQLiteDatabase; 
      import android.database.sqlite.SQLiteOpenHelper; 
        public class DataBaseWrapper extends SQLiteOpenHelper { 

        public static final String STUD = "Stud"; 
        public static final String STUDENT_ID = "_id"; 
        public static final String STUDENT_NAME = "_name"; 
        public static final String STUDENT_LOC = "_loc"; 

        private static final String DATABASE_NAME = "Students.db"; 
        private static final int DATABASE_VERSION = 1; 
          // creation SQLite statement 
        private static final String DATABASE_CREATE = "create table " + STUD 
         + "(" + STUDENT_ID + " integer primary key autoincrement, " 
          + STUDENT_NAME + " text not null," 
          + STUDENT_LOC + "text not null);"; 
          public DataBaseWrapper(Context context) { 
         super(context, DATABASE_NAME, null, DATABASE_VERSION); 
        } 

        @Override 
       public void onCreate(SQLiteDatabase db) { 
         db.execSQL(DATABASE_CREATE); 

        } 

        @Override 
       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
         // you should do some logging in here 
        // .. 

        db.execSQL("DROP TABLE IF EXISTS " + STUD); 
        onCreate(db); 
        } 

         } 

Student.java

package com.act; 
      public class Student { 
       private int id; 
       private String name; 
    private String loc; 
       public long getId() { 
       return id; 
      } 
        public void setId(int id) { 
        this.id = id; 
       } 



       public String getName() { 
       return this.name; 
       } 

       public void setName(String name) { 
        this.name = name; 
      } 


       public String getLoc() { 
        return this.loc; 
        } 

        public void setLoc(String loc) { 
         this.loc = loc; 
       } 
       @Override 
       public String toString() { 
       return name; 
       } 
      } 

StudentOperations.java

package com.act; 
    import java.util.ArrayList; 
    import java.util.List; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
public class StudentOperations { 
// Database fields 
private DataBaseWrapper dbHelper; 
private String[] STUDENT_TABLE_COLUMNS = { DataBaseWrapper.STUDENT_ID,  DataBaseWrapper.STUDENT_NAME,DataBaseWrapper.STUDENT_LOC }; 
private SQLiteDatabase database; 
public StudentOperations(Context context) { 
    dbHelper = new DataBaseWrapper(context); 
      } 
public void open() throws SQLException { 
    database = dbHelper.getWritableDatabase(); 
      } 
       public void close() { 
       dbHelper.close(); 
      } 
       public Student addStudent(String name, String loc) { 
        ContentValues values = new ContentValues(); 

       values.put(DataBaseWrapper.STUDENT_NAME, name); 
       values.put(DataBaseWrapper.STUDENT_LOC,loc); 
        long studId = database.insert(DataBaseWrapper.STUD, null, values); 

       // now that the student is created return it ... 
      Cursor cursor = database.query(DataBaseWrapper.STUD, 
         STUDENT_TABLE_COLUMNS, DataBaseWrapper.STUDENT_ID + " = " 
           + studId, null, null, null, null); 

       cursor.moveToFirst(); 

       Student newComment = parseStudent(cursor); 
       cursor.close(); 
      return newComment; 
      } 

      public void deleteStudent(Student comment) { 
       long id = comment.getId(); 
       System.out.println("Comment deleted with id: " + id); 
      database.delete(DataBaseWrapper.STUD, DataBaseWrapper.STUDENT_ID 
        + " = " + id, null); 
      } 
       public List getAllStudents() { 
      List students = new ArrayList(); 

       Cursor cursor = database.query(DataBaseWrapper.STUD, 
        STUDENT_TABLE_COLUMNS, null, null, null, null, null); 

      cursor.moveToFirst(); 
      while (!cursor.isAfterLast()) { 
       Student student = parseStudent(cursor); 
       students.add(student); 
        cursor.moveToNext(); 
      } 

       cursor.close(); 
       return students; 
     } 

      private Student parseStudent(Cursor cursor) { 
       Student student = new Student(); 
      student.setId((cursor.getInt(0))); 
       student.setName(cursor.getString(1)); 
       student.setLoc(cursor.getString(2)); 
       return student; 
      } 
     } 

XML代碼 activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:orientation="vertical" > 
       <EditText 
        android:id="@+id/editText1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_alignParentTop="true" 
         android:ems="10" > 

         <requestFocus /> 
        </EditText> 

       <Button 
         android:id="@+id/addBtn" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_below="@+id/editText1" 
         android:onClick="addUser" 
         android:text="Add New" /> 

        <Button 
         android:id="@+id/deleteBtn" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_toRightOf="@+id/addBtn" 
         android:layout_below="@+id/editText1" 
         android:onClick="deleteFirstUser" 
         android:text="Delete First" /> 

        <ListView 
         android:id="@android:id/list" 
         android:layout_width="match_parent" 
         android:layout_height="212dp" 
         android:layout_alignParentLeft="true" 
         android:layout_below="@+id/deleteBtn" > 
        </ListView> 

        <EditText 
         android:id="@+id/editText2" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" /> 

       </LinearLayout> 

的logcat:

02-27 05:13:53.349: D/dalvikvm(1057): GC_FOR_ALLOC freed 52K, 6% free 2878K/3036K, paused 35ms, total 37ms 
    02-27 05:13:53.389: D/dalvikvm(1057): GC_FOR_ALLOC freed 4K, 6% free 2972K/3136K, paused 26ms, total 26ms 
    02-27 05:13:53.399: I/dalvikvm-heap(1057): Grow heap (frag case) to 4.074MB for 1127536-byte allocation 
    02-27 05:13:53.509: D/dalvikvm(1057): GC_FOR_ALLOC freed <1K, 4% free 4073K/4240K, paused 108ms, total 108ms 
    02-27 05:13:53.650: E/SQLiteLog(1057): (1) near ")": syntax error 
    02-27 05:13:53.650: D/AndroidRuntime(1057): Shutting down VM 
    02-27 05:13:53.650: W/dalvikvm(1057): threadid=1: thread exiting with uncaught exception (group=0x414c4700) 
    02-27 05:13:53.670: E/AndroidRuntime(1057): FATAL EXCEPTION: main 
    02-27 05:13:53.670: E/AndroidRuntime(1057): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.act/com.act.MainActivity}: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: create table Stud(_id integer primary key autoincrement, _name text not null,); 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.os.Handler.dispatchMessage(Handler.java:99) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.os.Looper.loop(Looper.java:137) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at java.lang.reflect.Method.invokeNative(Native Method) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at java.lang.reflect.Method.invoke(Method.java:525) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at dalvik.system.NativeStart.main(Native Method) 
    02-27 05:13:53.670: E/AndroidRuntime(1057): Caused by: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: create table Stud(_id integer primary key autoincrement, _name text not null,); 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at com.act.DataBaseWrapper.onCreate(DataBaseWrapper.java:26) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at com.act.StudentOperations.open(StudentOperations.java:19) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at com.act.MainActivity.onCreate(MainActivity.java:19) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.app.Activity.performCreate(Activity.java:5133) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
    02-27 05:13:53.670: E/AndroidRuntime(1057):  
+0

嘗試我的下面的解決方案,讓我知道它是否工作與否。 – InnocentKiller

回答

1

入住此行

02-27 05:13:53.670: E/AndroidRuntime(1057): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.act/com.act.MainActivity}: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: create table Stud(_id integer primary key autoincrement, _name text not null,); 

改變這一行 創建表Stud(_id integer primary key autoincrement, _name text not null,);

create table Stud(_id integer primary key autoincrement, _name text not null); 
+0

它包含「,」查詢結束。我認爲這是語法錯誤。 – anuruddhika

0

我想你在STUDENT_LOC + "text not null);text not null之間錯過了空間,以便與此STUDENT_LOC + " text not null);讓你的整個查詢替換它應該像現在這樣,嘗試像下面。

此外,你錯過了關閉"在編輯STUDENT_LOC請驗證它。

private static final String DATABASE_CREATE = "create table " + STUD 
         + "(" + STUDENT_ID + " integer primary key autoincrement, " 
          + STUDENT_NAME + " text not null," 
          + STUDENT_LOC + " text not null ")"; 
          public DataBaseWrapper(Context context) { 
         super(context, DATABASE_NAME, null, DATABASE_VERSION); 
        } 
+0

你不認爲在''text not null'''之後''圓括號'和'分號'應該在'引號'內嗎?你已經遺漏了'sql語句'的結尾。 – DroidDev

相關問題