Insert data into the database by Alertdialog

Asked

Viewed 144 times

0

Good evening, I created a Alertdialog that asks the name of the player and puts in editText... when the user clicks to confirm it saved in the database... I made the code and it seems that I am not able to relate id or something like.

Could someone help me with this part please?

Code of the dialog:

package com.allsport.miyonic.allsport;


import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import Base.Esporte;

public class DialogGol extends DialogFragment{

    private EditText caixa;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){

        caixa = (EditText) caixa.findViewById(R.id.caixa_dialog_1);

        AlertDialog.Builder bir = new AlertDialog.Builder(getActivity());

        bir.setMessage("Gol de ").setPositiveButton("Confirmar", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                Esporte inseri = new Esporte();
                inseri.setjogadores(caixa.getText().toString());


            }
        }).setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //nulo
            }
        });

        bir.setTitle("Gooolll!!!");

        View view = LayoutInflater.from(getContext()).inflate(R.layout.caixa_jogador_dialog, null);
        bir.setView(view);

        AlertDialog dialog = bir.create();

        return dialog;
    }

}

editText XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/caixa_dialog_1"
            android:hint="Nome do jogador que fez o gol"
            android:layout_weight="1"
            android:gravity="center_vertical|center"
            android:shadowColor="@color/preto"
            android:textColor="@color/preto"/>
    </LinearLayout>
</LinearLayout>

Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.allsport.miyonic.allsport, PID: 2788
                  java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.EditText.findViewById(int)' on a null object reference
                      at com.allsport.miyonic.allsport.DialogGol.onCreateDialog(DialogGol.java:24)
                      at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:308)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Thank you...

  • Could you put the Sports object code? It is very likely that the problem is in this class.

  • @E.Thomas take a look at my Logcat... I edited my question

1 answer

1


You must change the

       caixa = (EditText) caixa.findViewById(R.id.caixa_dialog_1);

For

View view = LayoutInflater.from(getContext()).inflate(R.layout.caixa_jogador_dialog, null);
caixa = (EditText) view.findViewById(R.id.caixa_dialog_1);

'Cause you’re trying to call the findViewById() in a EditText

  • Thanks... it worked here, and every time I want to relate an id of a separate xml I have to make it right?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.