不幸的是,welcome停止在android中工作
我曾尝试在android studio中通过连接到sqlite来完成一个项目。按下按钮时,sqlite只存储年龄和姓名。但它正在停止 MainActivity.java不幸的是,welcome停止在android中工作,android,Android,我曾尝试在android studio中通过连接到sqlite来完成一个项目。按下按钮时,sqlite只存储年龄和姓名。但它正在停止 MainActivity.java package com.example.pallavi.hello; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.vie
package com.example.pallavi.hello;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.content.Intent;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
public void onButtonClick(View v){
EditText a=(EditText)findViewById(R.id.editText);
String str=a.getText().toString();
if(v.getId()==R.id.Bdisplay)
{
Intent i = new Intent(MainActivity.this,Display.class);
i.putExtra("Username",str);
startActivity(i);
}
if(v.getId()==R.id.SignInButton)
{
Intent i = new Intent(MainActivity.this,SignUp.class);
startActivity(i);
}
}
@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);
}
}
这是Connectdb.java,我在这里用于get和set。
Connectdb.java
package com.example.pallavi.welcome;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by pallavi on 12/5/15.
*/
public class Connectdb extends Activity {
String name,age;
public void setAge(String age) {
this.age = age;
}
public String getAge() {
return this.age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
主活动xml页面的代码,我从这里获取输入:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Age"
android:id="@+id/textView2"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onButtonClick" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
它甚至说太多的输出无法处理
logcat中的错误为:
05-15 14:29:52.924 1869-1869/com.example.pallavi.welcome E/SQLiteLog﹕ (1) near "auto_increment": syntax error
05-15 14:29:52.929 1869-1869/com.example.pallavi.welcome E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.pallavi.welcome, PID: 1869
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.database.sqlite.SQLiteException: near "auto_increment": syntax error (code 1): , while compiling: create table students (id integer primary key not null auto_increment,name text not null,age text not null);
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.pallavi.welcome.DatabaseHelper.onCreate(DatabaseHelper.java:29)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.pallavi.welcome.DatabaseHelper.insertContact(DatabaseHelper.java:38)
at com.example.pallavi.welcome.MainActivity.onButtonClick(MainActivity.java:39)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-15 14:29:52.924 1869-1869/com.example.pallavi.welcome E/SQLiteLog﹕ (1) 接近“自动增量”:语法错误
05-15 14:29:52.929 1869-1869/com.example.pallavi.welcome E/AndroidRuntime﹕ 致命异常:主
流程:com.example.pallavi.welcome,PID:1869
java.lang.IllegalStateException:无法执行活动的方法
在android.view.view$1.onClick上(view.java:4007)
在android.view.view.performClick上(view.java:4756)
在android.view.view$PerformClick.run(view.java:19749)
位于android.os.Handler.handleCallback(Handler.java:739)
位于android.os.Handler.dispatchMessage(Handler.java:95)
位于android.os.Looper.loop(Looper.java:135)
位于android.app.ActivityThread.main(ActivityThread.java:5221)
位于java.lang.reflect.Method.invoke(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
原因:java.lang.reflect.InvocationTargetException
位于java.lang.reflect.Method.invoke(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:372)
在android.view.view$1.onClick上(view.java:4002)
在android.view.view.performClick上(view.java:4756)
在android.view.view$PerformClick.run(view.java:19749)
位于android.os.Handler.handleCallback(Handler.java:739)
位于android.os.Handler.dispatchMessage(Handler.java:95)
位于android.os.Looper.loop(Looper.java:135)
位于android.app.ActivityThread.main(ActivityThread.java:5221)
位于java.lang.reflect.Method.invoke(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
造成原因:android.database.sqlite.SQLiteException:near“auto_increment”:语法错误(代码1),编译时:create table student(id integer主键不为null auto_increment,name text不为null,age text不为null);
位于android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法)
位于android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
位于android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
位于android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
位于android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)
位于android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31)
位于android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
位于android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
在com.example.pallavi.welcome.DatabaseHelper.onCreate(DatabaseHelper.java:29)上
位于android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
位于android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
位于com.example.pallavi.welcome.DatabaseHelper.insertContact(DatabaseHelper.java:38)
点击com.example.pallavi.welcome.MainActivity.onButtonClick(MainActivity.java:39)
位于java.lang.reflect.Method.invoke(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:372)
在android.view.view$1.onClick上(view.java:4002)
在android.view.view.performClick上(view.java:4756)
在android.view.view$PerformClick.run(view.java:19749)
位于android.os.Handler.handleCallback(Handler.java:739)
位于android.os.Handler.dispatchMessage(Handler.java:95)
位于android.os.Looper.loop(Looper.java:135)
位于android.app.ActivityThread.main(ActivityThread.java:5221)
位于java.lang.reflect.Method.invoke(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我猜在创建表时有语法错误,您的logcat中的错误也是可以理解的。尝试将auto\u increment
替换为AUTOINCREMENT
,这是sqlite中正确的关键字。您的日志显示
造成原因:android.database.sqlite.SQLiteException:near“auto_increment”:语法错误(代码1),编译时:create table student(id integer主键不为null auto_increment,name text不为null,age text不为null)
它应该是自动增量,而不是自动增量。您发布的错误清楚地表明:
Caused by: android.database.sqlite.SQLiteException: near "auto_increment":
syntax error (code 1): , while compiling:
create table students (id integer primary key not null auto_increment,name text not null,age text not null);
因此,错误源在以下字符串中:
private static final String TABLE_CREATE="create table students (id integer primary key not null auto_increment,"+"name text not null,age text not null); ";
查询错误,应该是(请注意,autoincrement
关键字应该在整数主键
之后):
有关更多详细信息,请查看。根据应用程序的名称筛选logcat,并发布相关错误I Get error more
private static final String TABLE_CREATE="create table students (id integer primary key not null auto_increment,"+"name text not null,age text not null); ";
create table students (id integer primary key autoincrement not null, name text not null,age text not null);