任何人都可以帮我解决我在Android项目中遇到的错误吗?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/addButton"
android:layout_alignParentTop="true"
android:textColor="#00f"
android:layout_gravity="center"
android:text="Student Records Application" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:layout_gravity="center"
android:text="Select What you would like to do" />
<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:layout_gravity="center"
android:text="Add New Records" />
<Button
android:id="@+id/editButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/addButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:layout_gravity="center"
android:text="Edit Student Records" />
<Button
android:id="@+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:layout_gravity="center"
android:text="Delete Student Records" />
</LinearLayout>
package com.example.studentrecord;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button addButton;
Button editButton;
Button deleteButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addButton=(Button)findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Going to add Record Activity", Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this, Add_Student.class));
}
});
editButton=(Button)findViewById(R.id.editButton);
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Going to Edit Record Activity", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_edit_student);
startActivity(new Intent(MainActivity.this, EditStudent.class));
}
});
deleteButton=(Button)findViewById(R.id.deleteButton);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Going to Delete Record Activity", Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this, Delete_Student.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
上面的布局和代码包含在主要活动中。
package com.example.studentrecord;
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 {
public static final String DB_NAME="student";
public static final String DB_TABLE="StudentInfo";
public static final int DB_VERSION=1;
private static final String CREATE_TABLE="CREATE TABLE" + DB_TABLE + "studentId INTEGER PRIMARY KEY, firstName TEXT, lastName TEXT, gender TEXT, course TEXT. age INTEGER);";
private SQLHelper helper;
private SQLiteDatabase db;
private Context context;
public DatabaseManager(Context c) {
this.context=c;
helper=new SQLHelper(c);
this.db=helper.getReadableDatabase();
}
public DatabaseManager openReadable() throws android.database.SQLException {
helper=new SQLHelper(context);
db=helper.getReadableDatabase();
return this;
}
public void close() {
helper.close();
}
public Boolean addRow(Integer a, String b, String c, String d, String e, Integer f) {
ContentValues newStudent = new ContentValues();
newStudent.put("studentId", a);
newStudent.put("firstName", b);
newStudent.put("lastName",c);
newStudent.put("gender",d);
newStudent.put("course",e);
newStudent.put("age", f);
try {
db.insertOrThrow(DB_TABLE, null, newStudent);
}
catch(Exception e1){
Log.e("Error in inserting row", e1.toString());
e1.printStackTrace();
return false;
}
db.close();
return true;
}
public String retrieveRows() {
String[] columns = new String[] {"studentId","firstName","lastName","gender","course","age"};
Cursor cursor=db.query(DB_TABLE, columns, null, null, null, null, null);
String tableRows="";
cursor.moveToFirst();
while (cursor.isAfterLast()==false) {
tableRows=tableRows + cursor.getInt(0) + "," + cursor.getString(1)+ "," +cursor.getString(2)+","+cursor.getString(3)+","+cursor.getString(4)+","+cursor.getInt(5) + "\n";
cursor.moveToNext();
}
if(cursor!=null && !cursor.isClosed()) {
cursor.close();
}
return tableRows;
}
public class SQLHelper extends SQLiteOpenHelper {
public SQLHelper(Context c) {
super(c, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
Log.w("Students table", "Upgrading database i.e. dropping table and recreating it");
}
}
}
上面的代码包含在我的数据库管理器文件中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="15sp"
android:textColor="#33f"
android:background="#cfcfcf"
android:text="Add a student Record" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Student Id" />
<EditText
android:id="@+id/studentId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="30dp"
android:inputType="number"
android:ems="10" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="First Name" />
<EditText
android:id="@+id/fName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="25dp"
android:inputType="text"
android:ems="10" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Last Name" />
<EditText
android:id="@+id/lName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:inputType="text"
android:layout_marginLeft="25dp"
android:ems="10" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Gender" />
<Spinner
android:id="@+id/spinnerGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:scrollbarSize="5sp"
android:layout_marginLeft="45dp"
android:entries="@array/Gender_Array"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Course" />
<Spinner
android:id="@+id/spinnerCourse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:scrollbarSize="5sp"
android:layout_marginLeft="45dp"
android:entries="@array/Course_Array"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Age" />
<EditText
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:inputType="number"
android:layout_marginLeft="60dp"
android:ems="10" >
</EditText>
</LinearLayout>
<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
android:text="Submit" />
<TextView
android:id="@+id/response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
package com.example.studentrecord;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class Add_Student extends Activity {
private DatabaseManager mydManager;
private TextView response;
private EditText studentId, fName, lName, age;
private Button submit;
private Boolean recordInsert;
private Spinner gender, course;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__student);
response=(TextView)findViewById(R.id.response);
submit=(Button)findViewById(R.id.buttonSubmit);
submit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast
Toast.makeText(Add_Student.this, "clicked" , Toast.LENGTH_LONG).show();
mydManager = new DatabaseManager(Add_Student.this);
studentId=(EditText)findViewById(R.id.studentId);
fName=(EditText)findViewById(R.id.fName);
lName=(EditText)findViewById(R.id.lName);
gender=(Spinner)findViewById(R.id.spinnerGender);
course=(Spinner)findViewById(R.id.spinnerCourse);
age=(EditText)findViewById(R.id.age);
recordInsert=mydManager.addRow(Integer.parseInt(studentId.getText().toString()), fName.getText().toString(), lName.getText().toString(), gender.getSelectedItem().toString(), course.getSelectedItem().toString(), Integer.parseInt(age.getText().toString()));
if (recordInsert){
response.setText("The row is inserted");
}
else
{
response.setText("Sorry, database could not be inserted");
}
InputMethodManager imm=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(studentId.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
mydManager.close();
studentId.setText("");
fName.setText("");
lName.setText("");
gender.setSelection(0);
course.setSelection(0);
age.setText("");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add__student, menu);
return true;
}
}
在我执行提交按钮之前,一切都很好,应用程序崩溃了。 有人能发现错误吗?
FYI控制台中没有错误。每当我执行提交按钮时,只有应用程序崩溃了 感谢
日志文件
09-22 22:44:50.737: D/dalvikvm(1297): GC_FOR_ALLOC freed 26K, 6% free 4192K/4428K, paused 19ms, total 20ms
09-22 22:44:50.767: D/dalvikvm(1297): GC_FOR_ALLOC freed 3K, 6% free 4445K/4692K, paused 20ms, total 21ms
09-22 22:44:52.657: I/Choreographer(1297): Skipped 42 frames! The application may be doing too much work on its main thread.
09-22 22:44:53.267: E/SQLiteLog(1297): (1) near "TABLEStudentInfostudentId": syntax error
09-22 22:44:53.267: D/AndroidRuntime(1297): Shutting down VM
09-22 22:44:53.267: W/dalvikvm(1297): threadid=1: thread exiting with uncaught exception (group=0xb3af6b90)
09-22 22:44:53.287: E/AndroidRuntime(1297): FATAL EXCEPTION: main
09-22 22:44:53.287: E/AndroidRuntime(1297): Process: com.example.studentrecord, PID: 1297
09-22 22:44:53.287: E/AndroidRuntime(1297): android.database.sqlite.SQLiteException: near "TABLEStudentInfostudentId": syntax error (code 1): , while compiling: CREATE TABLEStudentInfostudentId INTEGER PRIMARY KEY, firstName TEXT, lastName TEXT, gender TEXT, course TEXT. age INTEGER);
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
09-22 22:44:53.287: E/AndroidRuntime(1297): at com.example.studentrecord.DatabaseManager$SQLHelper.onCreate(DatabaseManager.java:77)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
09-22 22:44:53.287: E/AndroidRuntime(1297): at com.example.studentrecord.DatabaseManager.<init>(DatabaseManager.java:22)
09-22 22:44:53.287: E/AndroidRuntime(1297): at com.example.studentrecord.Add_Student$1.onClick(Add_Student.java:43)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.view.View.performClick(View.java:4424)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.view.View$PerformClick.run(View.java:18383)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.os.Handler.handleCallback(Handler.java:733)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.os.Handler.dispatchMessage(Handler.java:95)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.os.Looper.loop(Looper.java:137)
09-22 22:44:53.287: E/AndroidRuntime(1297): at android.app.ActivityThread.main(ActivityThread.java:4998)
09-22 22:44:53.287: E/AndroidRuntime(1297): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 22:44:53.287: E/AndroidRuntime(1297): at java.lang.reflect.Method.invoke(Method.java:515)
09-22 22:44:53.287: E/AndroidRuntime(1297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
09-22 22:44:53.287: E/AndroidRuntime(1297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
09-22 22:44:53.287: E/AndroidRuntime(1297): at dalvik.system.NativeStart.main(Native Method)
09-22 22:44:56.597: D/gralloc_goldfish(1332): Emulator without GPU emulation detected.
答案 0 :(得分:2)
您需要在CREATE TABLE和表名之间放置一个空格并修复一些其他语法错误,然后才能正确解析SQL:
private static final String CREATE_TABLE="CREATE TABLE " + DB_TABLE +
" (studentId INTEGER PRIMARY KEY, firstName TEXT, lastName TEXT, gender TEXT,
course TEXT, age INTEGER);";