不幸的是,欢迎停止在android工作

时间:2015-05-12 12:13:05

标签: android

我试图通过连接到sqlite.which在android studio中做一个项目,只需按下按钮时存储年龄和名字。但它正在停止..

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.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。 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>

DatabaseHelper.java

package com.example.pallavi.welcome;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by pallavi on 12/5/15.
 */
public class DatabaseHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION=3;
    private static final String DATABASE_NAME="students.db";
    private static final String TABLE_NAME="students";
    private static final String COLOUMN_ID="id";
    private static final String COLOUMN_NAME="name";
    private static final String COLOUMN_AGE="age";
    SQLiteDatabase db;

    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); ";

   public DatabaseHelper(Context context)
   {
       super(context,DATABASE_NAME,null,DATABASE_VERSION);
   }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLE_CREATE);
        this.db=db;



    }

    public void insertContact(Connectdb c)
    {
       db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(COLOUMN_NAME,c.getName());
        values.put(COLOUMN_AGE,c.getAge());

        db.insert(TABLE_NAME,null,values);

    }



    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String query="DROP TABLE IF EXISTS"+TABLE_NAME;
        db.execSQL(query);
        this.onCreate(db);

    }


}

甚至说要处理太多的输出 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)

3 个答案:

答案 0 :(得分:0)

我想在创建表时会出现语法错误,因为logcat中的错误也可以理解。请尝试将auto_increment替换为AUTOINCREMENT,这是sqlite中的正确关键字。

答案 1 :(得分:0)

你的Logcat说

  

引起:android.database.sqlite.SQLiteException:near&#34; auto_increment&#34 ;:语法错误(代码1):,同时编译:create table students(id integer primary key not null auto_increment,name text not not null,age text not null);

它应该是autoincrement而不是auto_increment。

答案 2 :(得分:0)

您发布的错误清楚地说明了:

 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之后的integer primary key关键字应该是正确的:

create table students (id integer primary key autoincrement not null, name text not null,age text not null);

有关详细信息,请查看here