[英]“Unfortunately app_name has stopped working”
The emulator does not respond to this code. 仿真器不响应此代码。
It gives the error as "Unfortunatey, Act has stopped ". 它给出错误为“不幸的是,行为已停止”。 I tried the same code with just one field as student name and it worked well but with two fields as student name and location its not responding.
我只用一个字段作为学生姓名尝试了相同的代码,但效果很好,但是用两个字段作为学生姓名和位置却没有响应。 Pls do help me out.
请帮我。
MainActivity.java 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 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 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 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 code activity_main.xml 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: 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):
Check this line 检查这条线
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,);
Change this line create table Stud(_id integer primary key autoincrement, _name text not null,);
更改此行,创建表
Stud(_id integer primary key autoincrement, _name text not null,);
as 如
create table Stud(_id integer primary key autoincrement, _name text not null);
I think you missed space at STUDENT_LOC + "text not null );
between text not null
so replace it with this STUDENT_LOC + " text not null );
我认为您错过了
STUDENT_LOC + "text not null );
text not null
之间text not null
因此,请用此STUDENT_LOC + " text not null );
替换它STUDENT_LOC + " text not null );
so your whole query should like this, try it like below. 因此您的整个查询都应该这样,请按以下方式尝试。
And also you missed to close the "
at ed of STUDENT_LOC
Please verify it. 而且你错过关闭
"
在编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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.