The app stops when trying to enter data into a database [closed]

0

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 !!

    
asked by Johan Rojas 27.05.2017 в 02:49
source

2 answers

1

The problem that occurs when clicking is that you are using context this , if you are in Fragment , remember to use the context of the Activity that loads the Fragment , use getActivity() :

UsuarioSQLiteHelper usuario = new UsuarioSQLiteHelper(getActivity(), "BDClientes",null,1 );
    
answered by 31.05.2017 в 04:16
0

If you can add the Logcat it would be very helpful to identify the problem. Still, I'm going to correct something that I see is wrong.

1st In UsuarioSQLiteHelper when you create the table, you are using:

String sql= "CREATE TABLE Cliente(nombre,TEXT,telefono,TEXT,direccion,TEXT,facebook,TEXT,debe,TEXT,pago,TEXT,fecha,TEXT)";

That should give you an error since you are repeating the table TEXT , modify it this way:

String sql= "CREATE TABLE if not exists Cliente(nombre,telefono,direccion,facebook,debe,pago,fecha)";

2nd In your Fragment the constructor of the SQL is in the absence of ' (quotes), having it this way:

db.execSQL( "INSERT INTO Cliente (nombre,telefono,direccion,facebook )VALUES("+ stretNom +","+stretTel+","+stretDir+","+stretFb+"')") ;

Modify it in the following way so that you do not get an error there:

db.execSQL( "INSERT INTO Cliente (nombre,telefono,direccion,facebook )VALUES('"+ stretNom +"','"+stretTel+"','"+stretDir+"','"+stretFb+"')") ;

3º The class SQLiteOpenHelper as the first argument needs the context, in your class, you have the following way:

public UsuarioSQLiteHelper(View.OnClickListener context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super( (Context) context, name, factory, version );
}

You are casting a ClickListener to Context . directly add the context of the application as follows:

public UsuarioSQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super( context, name, factory, version );
}

And having modified there, in your Fragment modify the this by getActivity () :

UsuarioSQLiteHelper usuario= new UsuarioSQLiteHelper( getActivity(), "BDClientes",null,1 );
    
answered by 27.05.2017 в 13:17