Java 将行插入SQLite数据库时Android应用程序未崩溃

Java 将行插入SQLite数据库时Android应用程序未崩溃,java,android,eclipse,sqlite,android-sqlite,Java,Android,Eclipse,Sqlite,Android Sqlite,大家好,我是Android开发的新手,我正在尝试制作一个应用程序,将用户的名字、姓氏、邮政编码、年龄和性别添加到SQLite数据库中,然后在下一页显示。我为此尝试了很多教程,但收效甚微。在EditText中输入数据后,按下submit按钮会导致android崩溃。如有任何帮助,将不胜感激。 这是我的主要活动,用户在其中输入数据 package com.example.votesmart; import android.os.Bundle; import android.app.Activit

大家好,我是Android开发的新手,我正在尝试制作一个应用程序,将用户的名字、姓氏、邮政编码、年龄和性别添加到SQLite数据库中,然后在下一页显示。我为此尝试了很多教程,但收效甚微。在EditText中输入数据后,按下submit按钮会导致android崩溃。如有任何帮助,将不胜感激。 这是我的主要活动,用户在其中输入数据

package com.example.votesmart;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class Zip extends Activity {

    private static final int TIME_ENTRY_REQUEST_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zip);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.zip, menu);
        return true;
    }

    public void onSubmit(View view){
        Intent intent = new Intent(this, EditProfile.class);
        startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);

        EditText firstView = (EditText)findViewById(R.id.fname_input);
        intent.putExtra("first", firstView.getText().toString());

        EditText lastView = (EditText)findViewById(R.id.lname_input);
        intent.putExtra("last", lastView.getText().toString());

        EditText zipView = (EditText)findViewById(R.id.zipcode_input);
        intent.putExtra("zip", zipView.getText().toString());

        EditText ageView = (EditText)findViewById(R.id.age_input);
        intent.putExtra("age", ageView.getText().toString());

        EditText sexView = (EditText)findViewById(R.id.sex_input);
        intent.putExtra("sex", sexView.getText().toString());
    }


}
其布局

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Zip"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="First Name" />

     <EditText
        android:id="@+id/fname_input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Last Name" />

     <EditText
        android:id="@+id/lname_input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Zip Code" />

    <EditText
        android:id="@+id/zipcode_input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Age" />

     <EditText
        android:id="@+id/age_input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Sex" />

     <EditText
        android:id="@+id/sex_input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/submit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:onClick="onSubmit" />

</LinearLayout>
我的适配器

package com.example.votesmart;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class VSAdapter extends CursorAdapter {

    public VSAdapter(Context context, Cursor cursor){
        super(context, cursor);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // TODO Auto-generated method stub
        TextView nameTextView = (TextView) view.findViewById(R.id.first_view);
        nameTextView.setText(cursor.getString(cursor.getColumnIndex("1")));
        TextView lastTextView = (TextView) view.findViewById(R.id.last_view);
        lastTextView.setText(cursor.getString(cursor.getColumnIndex("2")));
        TextView zipTextView = (TextView) view.findViewById(R.id.zip_view);
        zipTextView.setText(cursor.getString(cursor.getColumnIndex("3")));
        TextView ageTextView = (TextView) view.findViewById(R.id.age_view);
        ageTextView.setText(cursor.getString(cursor.getColumnIndex("4")));
        TextView sexTextView = (TextView) view.findViewById(R.id.sex_view);
        sexTextView.setText(cursor.getString(cursor.getColumnIndex("5")));
    }

    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.zip_list_item,  parent, false);


        return view;
    }

}
and finally my database class
private class DatabaseOpenHelper extends SQLiteOpenHelper{
        DatabaseOpenHelper(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            String CREATE_VOTERS_TABLE = "create table " + TABLE_NAME + "(" + KEY_ID + " integer primary key, " + KEY_FIRST + " text," + KEY_LAST + " text," + KEY_ZIP + " text," + KEY_AGE + " text," + KEY_SEX + "text" ;
            db.execSQL(
                    CREATE_VOTERS_TABLE
                    );
        }

        @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);
        }
    }

        public void addVoter(String first, String last, String zip, String age, String sex){
            //public void addVoter{Voter voter){
            ContentValues values = new ContentValues();
            values.put(KEY_FIRST, first);
            values.put(KEY_LAST, last);
            values.put(KEY_ZIP, zip);
            values.put(KEY_AGE, age);
            values.put(KEY_SEX, sex);

            db.insert(TABLE_NAME, null, values);

                //return db.update(TABLE_NAME, values, KEY_ID + " = ?", new String[] {String.valueOf(voter.getId()) });
            }
        public Cursor getAllRecords(){
            return db.rawQuery(
                    "select * from " + TABLE_NAME,
                    null
                    );
        }
}

再次感谢您的帮助,谢谢

这方面有一些问题

public void onSubmit(View view){
        Intent intent = new Intent(this, EditProfile.class);


        EditText firstView = (EditText)findViewById(R.id.fname_input);
        intent.putExtra("first", firstView.getText().toString());

        EditText lastView = (EditText)findViewById(R.id.lname_input);
        intent.putExtra("last", lastView.getText().toString());

        EditText zipView = (EditText)findViewById(R.id.zipcode_input);
        intent.putExtra("zip", zipView.getText().toString());

        EditText ageView = (EditText)findViewById(R.id.age_input);
        intent.putExtra("age", ageView.getText().toString());

        EditText sexView = (EditText)findViewById(R.id.sex_input);
        intent.putExtra("sex", sexView.getText().toString());
        startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);
    }
您需要在发送数据之前添加数据。使用onActivityResult的方法也是错误的

在EditProfile类的oncreate方法中,放置以下内容

Intent data = getIntent().getExtras();

String first = data.getStringExtra("first");
String last = data.getStringExtra("last");
String zip = data.getStringExtra("zip");
String age = data.getStringExtra("age");
String sex = data.getStringExtra("sex");
databaseHelper.addVoter(第一个、最后一个、邮政编码、年龄、性别)

理想情况下,这应该是一个线程,但是的

在这段代码中:

public void onSubmit(View view){
        Intent intent = new Intent(this, EditProfile.class);
        startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);
您正在第三行开始您的活动,但之后您将“添加数据”到意图中。。。这个意图被“星际触觉”所消耗。把它放在那个街区的底部


而且您不会在其他活动的“onResult”中获取意图数据,也就是在销毁活动之前设置活动结果的地方。收集的意图数据需要在显示活动之前-onCreate、onStart或onResume。OnCreate将执行一次。其他选项适用于您可能希望回收活动的情况(如在活动已启动后添加另一个投票人)。但是你需要检查是否有重复。

www.thnewboston.org上的好(android)SQLite教程谢谢你的输入,我试着把Intent data=getIntent().getExtras()放在上面;进入onCreate方法,并在将数据类型切换到bundle时出错。所以我把它设为Intent data=getIntent();data.getExtras();但我的应用程序没有变化,有什么建议吗?
public void onSubmit(View view){
        Intent intent = new Intent(this, EditProfile.class);
        startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);