簡體   English   中英

將行插入SQLite數據庫時Android App不會崩潰

[英]Android App not crashing when inserting row into SQLite database

嗨,我是Android開發的新手,正在嘗試制作一個將用戶的名字和姓氏,郵政編碼,年齡和性別都添加到SQLite數據庫中的應用程序,然后將其顯示在下一頁上。 我為此嘗試了許多教程,但收效甚微。 在EditText中輸入數據后,按下提交按鈕會導致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.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class EditProfile extends Activity {
    VSAdapter vsAdapter;

    public static final int TIME_ENTRY_REQUEST_CODE = 1;
    private DatabaseHandler databaseHelper;
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_profile);

        databaseHelper = new DatabaseHandler(this);

        ListView listView = (ListView)findViewById(R.id.list);
        vsAdapter = new VSAdapter(this, databaseHelper.getAllRecords());
        listView.setAdapter(vsAdapter); 
    }

    public void onBack (View view){
        finish();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == TIME_ENTRY_REQUEST_CODE){
            if (resultCode == RESULT_OK){

                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(first, last, zip, age, sex);
                //vsAdapter.changeCursor(databaseHelper.getAllRecords());

                //databaseHelper.addVoter(first, last, zip, age, sex);

            }
            }
        }
    }

我的適配器

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(first,last,zip,age,sex);

理想情況下,這應該在線程上,但是。

在此代碼塊中:

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

您在第三行開始活動,但是之后您將“添加數據”到該意圖中……該意圖已被“ startActivity”消耗。 將其放在該塊的底部。

而且,您不會在另一個活動的“ onResult”中獲得意圖數據-在銷毀活動之前,您要在其中設置活動的結果。 收集的意圖數據需要在顯示活動之前-onCreate,onStart或onResume。 OnCreate將執行一次。 其他選項則用於您可能希望回收活動的情況(例如在啟動后添加另一個選民)。 但是您需要檢查是否有欺騙。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM