Good night, I'm having problems in calling a dialog fragment in android, with an activity that is encoded in Kotlin, calling a Java-encoded fragment dialog, the NPE error is happening in the line "datePicker = getActivity (). getLayoutInflater (). Could you help me organize this code so I can launch the dialog with a date picker please? CadastroActivity
class CadastroActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cadastro)
button_nascimento.setOnClickListener {
var dataDialog : FragmentDialogDataPicker = FragmentDialogDataPicker()
dataDialog.show(supportFragmentManager, "Data Fragment")
}
}
}
Dialog
public class FragmentDialogDataPicker extends DialogFragment {
private DatePicker datePicker;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//Selecionando o xml de layout
LayoutInflater inflater = getActivity().getLayoutInflater();
//Iniciando o datePicker
datePicker = getActivity().findViewById(R.id.datepicker_nacimento);
Calendar calendar = Calendar.getInstance();
datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
//IMPLEMENTANDO A AÇÃO AO SELECIONAR A DATA NO CALENDÁRIO VIEW
Toast.makeText(getContext(), datePicker.getDayOfMonth() + " - " +
datePicker.getMonth() + " - " + datePicker.getYear(), Toast.LENGTH_SHORT).show();
}
});
//Setando as configurações do dialog
builder.setView(inflater.inflate(R.layout.fragment_dialog_data_picker, null))
.setPositiveButton(getString(R.string.selecionar), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Ação ao selecionar a data de nascimento
}
});
return builder.create();
}
}
xml activity
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/nome_completo"
android:inputType="text" />
<EditText
android:id="@+id/edit_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:layout_marginTop="@dimen/margim_top_edits" />
<Button
android:id="@+id/button_nascimento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/data_de_nascimento"
android:layout_marginTop="@dimen/margim_top_edits"
android:layout_gravity="center"/>
<Button
android:id="@+id/button_localizacao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Habilitar Localização"
android:layout_gravity="center"
android:layout_marginTop="@dimen/margim_top_edits"/>
<Button
android:id="@+id/button_cadastrar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cadastrar"
android:layout_marginTop="@dimen/margim_top_edits" />
</LinearLayout>
xml dialog
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<DatePicker
android:id="@+id/datepicker_nacimento"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>