I'm new to programming in android studio, I try to make an application that manages a database of SQLite however at the time of trying to record data the app stops. These are my codes: NOTE: I am working with fragments.
XML fragment:
<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:orientation="vertical"
tools:context=".VentaFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView15"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
android:text="Registro:"/>
<TextView
android:id="@+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="Nombre:"/>
<EditText
android:id="@+id/TxtNombre"
android:layout_width="325dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Nombre"
android:inputType="text"/>
<TextView
android:id="@+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="Telefono:"/>
<EditText
android:id="@+id/TxtTelefono"
android:layout_width="334dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="Telefono"/>
<TextView
android:id="@+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="Direccion:"/>
<EditText
android:id="@+id/TxtDireccion"
android:layout_width="332dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="Direccion"/>
<TextView
android:id="@+id/textView14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="Facebook:"/>
<EditText
android:id="@+id/Txtfb"
android:layout_width="328dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="Facebook"/>
<Button
android:id="@+id/BtnRegistrar"
android:layout_width="208dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Registrar"/>
</LinearLayout>
Code java del fragment:
package apps.rojas.proyecto_bd;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ClienteFragment extends Fragment {
EditText etNom,etTel,etDir,etFb;
Button btnAregis;
public ClienteFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_cliente,container,false);
etNom = (EditText) v.findViewById( R.id.TxtNombre );
etTel = (EditText) v.findViewById( R.id.TxtTelefono );
etDir= (EditText) v.findViewById( R.id.TxtDireccion );
etFb = (EditText) v.findViewById( R.id.Txtfb );
btnAregis = (Button) v.findViewById( R.id.BtnRegistrar );
btnAregis.setOnClickListener( new View.OnClickListener(){
public void onClick(View v) {
String stretNom= etNom.getText().toString();
String stretTel= etTel.getText().toString();
String stretDir= etDir.getText().toString();
String stretFb= etFb.getText().toString();
UsuarioSQLiteHelper usuario= new UsuarioSQLiteHelper( this, "BDClientes",null,1 );
SQLiteDatabase db= usuario.getWritableDatabase();
db.execSQL( "INSERT INTO Cliente (nombre,telefono,direccion,facebook )VALUES("+ stretNom +","+stretTel+","+stretDir+","+stretFb+"')") ;
db.close();
Toast.makeText(getActivity(),"Registro Completado con Exito",Toast.LENGTH_SHORT).show();
etNom.setText( "" );
etTel.setText( "" );
etDir.setText( "" );
etFb.setText( "" );
}
});
return v;
}
}
>
Codigo java de la Base de Datos:
>package apps.rojas.proyecto_bd;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.View;
public class UsuarioSQLiteHelper extends SQLiteOpenHelper {
String sql= "CREATE TABLE Cliente(nombre,TEXT,telefono,TEXT,direccion,TEXT,facebook,TEXT,debe,TEXT,pago,TEXT,fecha,TEXT)";
public UsuarioSQLiteHelper(View.OnClickListener context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super( (Context) context, name, factory, version );
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL( sql );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL( "DROP TABLE IF EXISTS Cliente" );
db.execSQL( sql );
}
}
I hope you can help me. The error is in the "BtnRegistrar" at the moment of clicking the app closes.
Beforehand, THANKS !!