当我运行应用程序时,我得到:跳过90帧。应用程序可能在其主线程上工作太多。
主要代码:
public class Student_Data extends Activity implements OnClickListener {
EditText studentName, StuClass;
Button b1;
String NameStudent;
String ClassStudent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student__data);
studentName= (EditText) findViewById(R.id.editText1);
StuClass= (EditText) findViewById(R.id.editText2);
NameStudent = studentName.getText().toString();
ClassStudent = StuClass.getText().toString();
b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//TODO Auto-generated method stub
insertData(NameStudent, ClassStudent);
}
private void insertData(String nameStudent2, String classStudent2) {
// TODO Auto-generated method stub
AndroidDbHelper helper = new AndroidDbHelper(this);
SQLiteDatabase database = helper.getWritableDatabase();
ContentValues content = new ContentValues();
content.put(AndroidDbHelper.COLUMN_NAME, NameStudent);
content.put(AndroidDbHelper.COLUMN_CLASS, ClassStudent);
database.insert(AndroidDbHelper.TABLE_NAME, null, content);
}
}
AndroidDbHelper.java
public class AndroidDbHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "StudentData";
public static final int DB_VERSION = 1;
public static final String TABLE_NAME = "student";
public static final String COLUMN_NAME= "name";
public static final String COLUMN_CLASS= "cclass";
public AndroidDbHelper(Context context) {
super(context, DB_NAME, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
String CreateTable = "CREATE TABLE" + TABLE_NAME + "(" + COLUMN_NAME + "TEXT," + COLUMN_CLASS + "TEXT" + ");";
// TODO Auto-generated method stub
db.execSQL(CreateTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME );
onCreate(db);
}
}
虽然,我正在获取主要活动页面以在模拟器上插入值。 但如果我插入值,我会在logcat中收到错误。
04-02 11:31:08.095: E/AndroidRuntime(674): FATAL EXCEPTION: main
04-02 11:31:08.095: E/AndroidRuntime(674): android.database.sqlite.SQLiteException: near "TABLEstudent": syntax error (code 1): , while compiling: CREATE TABLEstudent(nameTEXT,cclassTEXT);
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
04-02 11:31:08.095: E/AndroidRuntime(674): at com.example.student_data.AndroidDbHelper.onCreate(AndroidDbHelper.java:25)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
04-02 11:31:08.095: E/AndroidRuntime(674): at com.example.student_data.Student_Data.insertData(Student_Data.java:41)
04-02 11:31:08.095: E/AndroidRuntime(674): at com.example.student_data.Student_Data.onClick(Student_Data.java:36)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.view.View.performClick(View.java:4084)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.view.View$PerformClick.run(View.java:16966)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.os.Handler.handleCallback(Handler.java:615)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.os.Handler.dispatchMessage(Handler.java:92)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.os.Looper.loop(Looper.java:137)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-02 11:31:08.095: E/AndroidRuntime(674): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 11:31:08.095: E/AndroidRuntime(674): at java.lang.reflect.Method.invoke(Method.java:511)
04-02 11:31:08.095: E/AndroidRuntime(674): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-02 11:31:08.095: E/AndroidRuntime(674): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-02 11:31:08.095: E/AndroidRuntime(674): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student__data);
studentName= (EditText) findViewById(R.id.editText1);
StuClass= (EditText) findViewById(R.id.editText2);
b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
NameStudent = studentName.getText().toString();
ClassStudent = StuClass.getText().toString();
insertData(NameStudent, ClassStudent);
}
你在OnCreate Time获取String将始终为你提供空字符串。将它写入onClick方法将为您提供完美的输出。
String CreateTable = "CREATE TABLE" + TABLE_NAME + "(" + COLUMN_NAME + "TEXT," + COLUMN_CLASS + "TEXT" + ");";
将此行替换为此
String CreateTable = "CREATE TABLE " + TABLE_NAME + " ( " + COLUMN_NAME + " TEXT, " + COLUMN_CLASS + " TEXT" + ");";
答案 1 :(得分:0)
在onCreate()
方法中,您的EditTexts为空,因此我建议您将两行移动到onClick()方法。
按如下方式更改onClick()
方法,
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
NameStudent = studentName.getText().toString(); //move these lines from onCreate() method to here
ClassStudent = StuClass.getText().toString();
insertData(NameStudent, ClassStudent);
}
答案 2 :(得分:0)
你的SQL错了。
的结果String CreateTable = "CREATE TABLE" + TABLE_NAME + "(" + COLUMN_NAME + "TEXT," + COLUMN_CLASS + "TEXT" + ");";
是
CREATE TABLEStudentData(nameTEXT, cclassTEXT)
您需要添加空格
String CreateTable = "CREATE TABLE " + TABLE_NAME + " ( " + COLUMN_NAME + " TEXT, " + COLUMN_CLASS + " TEXT " + " );";