0

我正在使用java开发一个android应用程序。我使用 SQLite 创建了数据库。我正在尝试检查值是否已插入数据库。但是当点击注册按钮时,它应该会显示一条成功的消息。但没有任何反应,我没有收到任何错误。我检查了按钮 ID 并检查了几次代码,但我不明白为什么没有显示“成功”消息。请帮忙

    ---------------Class that Creates the Database------------------------------

    package com.android.disasterAlertApp;
    import android.content.*;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.*;

    public class DBAdapter {


    //Table where registration details will be saved
    public static final String KEY_USERNO = "userID";
    public static final String KEY_FIRSTNAME = "first_Name";
    public static final String KEY_LASTNAME = "last_Name";
    public static final String KEY_EMAIL = "email";
    public static final String KEY_MOBILENUMBER = "mobile_Number";
    public static final String KEY_LOCATIONUM = "locationID";

    private static final String DATABASE_NAME = "DisasterAlertDB";
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_TABLE = "User";

    private DbHelper ourhelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase; 



    public static class DbHelper extends SQLiteOpenHelper{

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE" + DATABASE_TABLE + " ("+ KEY_USERNO + "INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_FIRSTNAME + "TEXT NOT NULL, " + KEY_LASTNAME + "TEXT NOT NULL, " + KEY_EMAIL + "TEXT NOT NULL, " + KEY_MOBILENUMBER + "TEXT NOT NULL, " + KEY_LOCATIONUM + "TEXT NOT NULL);");
       }

        @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
        }

        public DBAdapter(Context c){
        ourContext = c;
    }

       public DBAdapter open() throws SQLException{
        ourhelper = new DbHelper(ourContext);
        ourDatabase = ourhelper.getWritableDatabase();
        return this; 
    }
       public void close(){
        ourhelper.close();
    }
       public long createEntry(String fname, String lname, String email,
        String number, String loc) {
    ContentValues cv = new ContentValues();
    cv.put(KEY_FIRSTNAME, fname);
    cv.put(KEY_LASTNAME, lname);
    cv.put(KEY_EMAIL, email);
    cv.put(KEY_MOBILENUMBER, number);
    cv.put(KEY_LOCATIONUM, loc);

    return ourDatabase.insert(DATABASE_TABLE, null, cv);
    }
}

------Class that gets the values from text fields and passes them to create a record-------

    package com.android.disasterAlertApp;

    import android.app.Activity;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class Registration extends Activity implements OnClickListener{
        Button sqlRegister;
        EditText sqlFirstName,sqlLastName,sqlEmail,sqlMobileNumber,sqlCurrentLocation,sqlUsername,sqlPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration);
        sqlFirstName = (EditText) findViewById(R.id.etFname);
        sqlLastName = (EditText) findViewById(R.id.etLname);
        sqlEmail = (EditText) findViewById(R.id.etEmail);
        sqlMobileNumber = (EditText) findViewById(R.id.etPhone);
        sqlCurrentLocation = (EditText) findViewById(R.id.etCurrentLoc);
        sqlUsername = (EditText) findViewById(R.id.etUsername);
        sqlPassword = (EditText) findViewById(R.id.etPwd);

        sqlRegister = (Button) findViewById(R.id.bRegister);
        sqlRegister.setOnClickListener(this);
    }
    public void onClick(View arg0) {
        switch (arg0.getId()){
        case R.id.bRegister:
            boolean worked = true;
            try{
            String fname = sqlFirstName.getText().toString();
            String Lname = sqlLastName.getText().toString();
            String email = sqlEmail.getText().toString();
            String number = sqlMobileNumber.getText().toString();
            String loc = sqlCurrentLocation.getText().toString();
            String uname = sqlUsername.getText().toString();
            String pwd = sqlPassword.getText().toString();

            DBAdapter entry = new DBAdapter(Registration.this); 
            entry.open();
            entry.createEntry(fname,Lname,email,number,loc);
            entry.close();
            }catch(Exception e){
                worked=false;
            }finally{
                if(worked){ //Gets fired when values are inserted
                    Dialog d = new Dialog(this);
                    d.setTitle("works");
                    TextView tv = new TextView(this);
                    tv.setText("Success");
                    d.setContentView(tv);
                    d.show();
                }
            }
            break;
            }
        }

      }

 ------------------Modified OnClick Method-------------------------------

public void onClick(View arg0) {
    Toast.makeText(this, "fire", Toast.LENGTH_LONG).show();
    switch (arg0.getId()){
    case R.id.bRegister:
        boolean worked = true;
        try{
        String fname = sqlFirstName.getText().toString();
        String Lname = sqlLastName.getText().toString();
        String email = sqlEmail.getText().toString();
        String number = sqlMobileNumber.getText().toString();
        String loc = sqlCurrentLocation.getText().toString();

        DBAdapter entry = new DBAdapter(Registration.this); 
        entry.open();
        entry.createEntry(fname,Lname,email,number,loc);
        entry.close();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(worked){
                Dialog d = new Dialog(this);
                d.setTitle("works");
                TextView tv = new TextView(this);
                tv.setText("Success");
                d.setContentView(tv);
                d.show();
            }
        }
        break;
    }
    }

    }
4

2 回答 2

0

您肯定会发生异常。捕获所有异常但未记录任何消息

}catch(Exception e){
            worked=false;
        }

工作的布尔值必须为假,因此您的对话框不会显示。

于 2012-06-07T13:51:34.580 回答
0
}catch(Exception e){
                worked=false;
            }finally{
                if(worked){ //Gets fired when values are inserted
                    Dialog d = new Dialog(this);
                    d.setTitle("works");
                    TextView tv = new TextView(this);
                    tv.setText("Success");
                    d.setContentView(tv);
                    d.show();
                }
            }

从代码中可以清楚地看出,它永远不会在失败时报告错误。

第 1 点只有在工作 = true 时才会显示消息。

第 2 点

catch(Exception e){
                    worked=false;
                }

你应该把 e.printStackTrace() 放在这里。

于 2012-06-07T13:50:34.010 回答