2012-06-07 46 views
0

我正在開發一個使用java的android應用程序。我使用SQLite創建了數據庫。我試圖檢查值是否被插入到數據庫中。但是當註冊按鈕被點擊時,它應該顯示一條消息說成功。但沒有任何反應,我沒有得到任何錯誤。我檢查了按鈕ID,並檢查了幾次代碼,但我不明白爲什麼沒有顯示「成功」消息。請幫助在Android應用程序中單擊按鈕時沒有任何反應

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

    } 
+0

你確定'if(working){}'是否被觸發?把'else並檢查?' –

+0

你的數據庫的路徑是什麼?你上面沒有提到任何變量。 – SALMAN

+0

@SALMAN亞,我已經給它那裏。 DBAdapter entry = new DBAdapter(Registration.this); entry.open(); entry.createEntry(fname,Lname,email,number,loc); entry.close(); –

回答

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

它從代碼它永遠不會失敗報告錯誤清除。

Point 1只有在工作= true時才顯示消息。

點2

catch(Exception e){ 
        worked=false; 
       } 

你就應該把e.printStackTrace()在這裏。

+0

我試着在切換後的OnClick方法中顯示一條消息。即使沒有顯示。這意味着OnClick方法未被觸發。怎麼了?爲什麼不啓動OnClick方法? –

+0

在此處更新您的修改後的代碼。 –

+0

請檢查我的編輯 –

0

你當然有一個異常發生。 所有的異常被逮住,但沒有消息記錄

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

的工作布爾一定是假的,因此不顯示您的對話框。

相關問題