Java 带有片段的DatePickerDialog问题
我有一个editText片段,用于打开日期选择器。我做的每件事似乎都是正确的,但是我不明白为什么会出现以下错误 这与我正在处理一个片段有关 你能帮我解决这个错误吗 EditProfileFragment.javaJava 带有片段的DatePickerDialog问题,java,android,Java,Android,我有一个editText片段,用于打开日期选择器。我做的每件事似乎都是正确的,但是我不明白为什么会出现以下错误 这与我正在处理一个片段有关 你能帮我解决这个错误吗 EditProfileFragment.java import android.app.DatePickerDialog; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle;
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;
}
}
错误日志
/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)
片段布局
<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>
它只是告诉您
DatePickerDialog
构造函数的第一个参数与预期的不一样;在这里,您放置了一个片段
,而它需要一个上下文
,所以在下面的行中替换
DatePickerDialog dialog = new DatePickerDialog(
EditProfileFragment.this,
android.R.style.Theme_DeviceDefault_Dialog_MinWidth,
mDateSetListener,
year, month, day);
与
使用getapplicationcontext()并删除Edittextprofilefragment。我如您所述执行了此操作,但是,现在出现以下错误。。。mDisplayDate=view.findViewById(R.id.etDateOfBirth);无法访问StationHi@Joe请放置
返回视图
在onCreateView()
方法的末尾作为最后一行代码好的,我把它放在那里,错误消失了,但是现在应用程序崩溃了。几秒钟后,我将更新代码并将错误添加到问题中。您可以共享片段布局吗?现在从com.google.android.material.textfield.TextInputLayout
移动com.google.android.material.textfield.TextInputLayout
,并将其放入com.google.android.material.textfield.textInputText
DatePickerDialog dialog = new DatePickerDialog(
requireContext(),
android.R.style.Theme_DeviceDefault_Dialog_MinWidth,
mDateSetListener,
year, month, day);