HELP PLEASE: Problem with onClick Listener

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

HELP PLEASE: Problem with onClick Listener

Postby spainmas » Fri Dec 11, 2009 12:48 pm

I'm writing a simple program to keep the names and telephone numbers of your contacts on a SQLite Database. It has 3 EditText and a button. When the user click the button, the information of each field will be kept as a registry in the database table: "contactos". When I execute the application I found this error: 'The application agenda_SQLite has stopped unexpectedly.Please try again'. I think the problem is the use of the onClickListener, because if I omit the line :"boton.setOnClickListener(this); " everything is all right . I'm desperate, I can' t see what is going wrong. Sorry for my English.



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package prueba.sqlite;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.view.View.OnClickListener;
  9.  
  10. import android.database.sqlite.SQLiteDatabase;
  11.  
  12. import android.os.Bundle;
  13.  
  14. import android.view.View;
  15.  
  16. import android.widget.Button;
  17.  
  18. import android.widget.EditText;
  19.  
  20.  
  21.  
  22. public class agenda extends Activity implements OnClickListener
  23.  
  24. {
  25.  
  26.            private final String NOMBRE_BBDD = "BBDD_agenda";
  27.  
  28.            private final String NOMBRE_TABLA = "contactos";
  29.  
  30.            private EditText texto_nombre;  
  31.  
  32.            private EditText texto_fijo;
  33.  
  34.            private EditText texto_movil;
  35.  
  36.            private Button boton;
  37.  
  38.            SQLiteDatabase BBDD;
  39.  
  40.    
  41.  
  42.            
  43.  
  44.     @Override
  45.  
  46.     public void onCreate(Bundle savedInstanceState)
  47.  
  48.     {
  49.  
  50.         super.onCreate(savedInstanceState);
  51.  
  52.         this.texto_nombre=(EditText)findViewById(R.id.texto_nombre);
  53.  
  54.         texto_fijo=(EditText)findViewById(R.id.texto_fijo);
  55.  
  56.         texto_movil=(EditText)findViewById(R.id.texto_movil);
  57.  
  58.         boton = (Button)findViewById(R.id.boton);
  59.  
  60.         boton.setOnClickListener(this);
  61.  
  62.        
  63.  
  64.         /* Abrimos la base de datos.
  65.  
  66.          * Si no existía previamente se creará automáticamente. */
  67.  
  68.          BBDD = this.openOrCreateDatabase(NOMBRE_BBDD, 1, null);
  69.  
  70.         /* Creamos la tabla de contactos en la base de datos.
  71.  
  72.          * En caso de que existiera previamente se producirá error ('IF NOT EXISTS'). */
  73.  
  74.         BBDD.execSQL("CREATE TABLE IF NOT EXISTS "
  75.  
  76.               + NOMBRE_TABLA + " (nombre VARCHAR, tel_fijo INT(9), tel_movil INT(9));");      
  77.  
  78.        
  79.  
  80.  
  81.  
  82.         setContentView(R.layout.main);
  83.  
  84.     }
  85.  
  86.    
  87.  
  88.     // Implement the OnClickListener callback
  89.  
  90.     public void onClick(View v) {
  91.  
  92.             String nombre=this.texto_nombre.getText().toString();
  93.  
  94.         int tel_fijo=Integer.parseInt(this.texto_fijo.getText().toString());
  95.  
  96.         int tel_movil=Integer.parseInt(this.texto_movil.getText().toString());
  97.  
  98.        
  99.  
  100.        
  101.  
  102.         BBDD.execSQL("INSERT INTO " + NOMBRE_BBDD  + " (nombre, tel_fijo, tel_movil)" +
  103.  
  104.                              " VALUES ('"+nombre+"', "+tel_fijo+", "+tel_movil+");");
  105.  
  106.     }
  107.  
  108.  
  109.  
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
spainmas
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Dec 11, 2009 12:30 pm

Top

Postby jbrohan » Mon Dec 14, 2009 10:20 pm

This is an issue dealt with in the Notepad tutorial. As I understand it in an onClickListener you write a routine that will be executed when that button is clicked. the routine public void onClick(View arg0) is teh routine which is called.
Hope this helps
John

public void okButtonCallback(int buttonref)
{
Button btnOk = (Button) findViewById(buttonref);
btnOk.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String s = persistWidgetData();
Intent i = getIntent();
Bundle bundle = new Bundle();
bundle.putString("returnValue",s);
bundle.putInt("TestIntReturn",3142);
i.putExtras(bundle); // load parameters into Intent
Log.e("SettingsDialog", String.format("in okButtonCallback = %s ",s));
setResult(RESULT_OK,i);
SettingsDialog.this.finish();
}
});

}
jbrohan
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri Aug 28, 2009 11:12 am
Location: Montreal

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 2 guests