Reputation: 343
I have a fragment with editText that is meant to open a date picker. I have done everything seemingly correctly, however I can't figure out why I get the following error.
It has something to do with the fact that I'm working with a fragment.
Could you please help with this error?
EditProfileFragment.java
import android.app.DatePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import com.archive.pod.R;
import com.google.android.material.textfield.TextInputEditText;
import java.util.Calendar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
public class EditProfileFragment extends Fragment {
//Initializing
private TextInputEditText mDisplayDate;
private DatePickerDialog.OnDateSetListener mDateSetListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_editprofile, container, false);
//EditText
mDisplayDate = view.findViewById(R.id.etDateOfBirth);
//Date picker dialog box
mDisplayDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(
requireContext(),
android.R.style.Theme_DeviceDefault_Dialog_MinWidth,
mDateSetListener,
year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
String date = month + "/" + day + "/" + year;
mDisplayDate.setText(date);
}
};
return view;
}
}
Error Log
/AndroidRuntime: FATAL EXCEPTION: main
Process: com.archive.pod, PID: 20399
java.lang.ClassCastException: com.google.android.material.textfield.TextInputLayout cannot be cast to com.google.android.material.textfield.TextInputEditText
at com.archive.pod.Profile.EditProfileFragment.onCreateView(EditProfileFragment.java: 33)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java: 2600)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java: 881)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java: 1238)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java: 434)
Fragment Layout
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/userProfilePicture"
android:src="@drawable/ic_profile_picture_default"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true" />
<ImageButton
android:id="@+id/changeUserPhoto"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="90dp"
android:layout_marginLeft="200dp"
android:layout_marginStart="200dp"
android:elevation="10dp"
android:background="@drawable/ic_add_circle"/>
<RelativeLayout
android:id="@+id/sectionTitle1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/changeUserPhoto"
android:layout_marginTop="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textSize="12sp"
android:textColor="@color/label"
android:text="Primary Information"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/relLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_below="@+id/sectionTitle1">
<!-- Full name input -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/etFullname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/full_name" />
</com.google.android.material.textfield.TextInputLayout>
<!-- Email input -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/etFullname"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/emailAddress"
android:inputType="textEmailAddress"/>
</com.google.android.material.textfield.TextInputLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/sectionTitle2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/relLayout1"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textSize="12sp"
android:textColor="@color/label"
android:text="Private Information"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/sectionTitle2">
<!-- Phone number input -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/etPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:longClickable="false"
android:focusableInTouchMode="false"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/phone_number"
android:inputType="date"/>
</com.google.android.material.textfield.TextInputLayout>
<!-- Date of birth input -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/etDateOfBirth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/etPhoneNumber"
android:focusable="false"
android:longClickable="false"
android:focusableInTouchMode="false"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/dob"
android:inputType="date"/>
</com.google.android.material.textfield.TextInputLayout>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</merge>
Upvotes: 1
Views: 83
Reputation: 40810
It just tells you that the first parameter of the DatePickerDialog
constructor is not as supposed to be; where you put a Fragment
, while it expects a context
, so replace below line
DatePickerDialog dialog = new DatePickerDialog(
EditProfileFragment.this,
android.R.style.Theme_DeviceDefault_Dialog_MinWidth,
mDateSetListener,
year, month, day);
with
DatePickerDialog dialog = new DatePickerDialog(
requireContext(),
android.R.style.Theme_DeviceDefault_Dialog_MinWidth,
mDateSetListener,
year, month, day);
Upvotes: 2