英文:
Date Picker Android Studio crash Error inflating class android.widget.DatePicker
问题
以下是您提供的XML和Java代码的翻译部分:
这是我的xml(非常简单,只有一个文本和一个提示文本,点击以选择日期):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context=".moreUserInfo">
<TextView
android:id="@+id/textView10"
android:layout_width="match_parent"
android:layout_height="97dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:gravity="center"
android:text="输入您的生日"
android:textColor="#ffffff"
android:textSize="20sp" />
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="3dp"
android:layout_marginTop="91dp"
android:layout_marginEnd="-3dp"
android:gravity="center"
android:hint="选择日期"
android:textColor="#ffffff"
android:textColorHint="#CEA6FFCC"
android:textSize="20sp" />
</RelativeLayout>
这是我的.java类:
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import java.util.Calendar;
import static android.R.style.Widget_Holo_ActionBar_Solid;
public class moreUserInfo extends AppCompatActivity {
private static final String TAG = "moreUserInfo";
private TextView mDisplayDate;
private DatePickerDialog.OnDateSetListener mDateSetListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_more_user_info);
mDisplayDate = (TextView) findViewById(R.id.tvDate);
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(moreUserInfo.this, Widget_Holo_ActionBar_Solid, mDateSetListener, year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 使背景透明。
dialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
Log.d(TAG, "onDateSet: date: " + i + "/" + i1 + "/" + i2);
}
};
}
}
请注意,这只是代码的翻译部分,不包括完整的解决方案或问题的答案。如果您需要有关代码问题的帮助,请提出具体问题。
英文:
I know similar questions have been asked but they didn't seem to solve this particular case.
this is my xml (very simple just a text and a hint text to click to select date):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context=".moreUserInfo">
<TextView
android:id="@+id/textView10"
android:layout_width="match_parent"
android:layout_height="97dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:gravity="center"
android:text="Enter your birthday"
android:textColor="#ffffff"
android:textSize="20sp" />
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="3dp"
android:layout_marginTop="91dp"
android:layout_marginEnd="-3dp"
android:gravity="center"
android:hint="Select Date"
android:textColor="#ffffff"
android:textColorHint="#CEA6FFCC"
android:textSize="20sp" />
</RelativeLayout>
this is my .java class:
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import java.util.Calendar;
import static android.R.style.Widget_Holo_ActionBar_Solid;
public class moreUserInfo extends AppCompatActivity {
private static final String TAG = "moreUserInfo";
private TextView mDisplayDate;
private DatePickerDialog.OnDateSetListener mDateSetListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_more_user_info);
mDisplayDate = (TextView) findViewById(R.id.tvDate);
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(moreUserInfo.this, Widget_Holo_ActionBar_Solid, mDateSetListener, year,month,day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); //make transparent background.
dialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
Log.d(TAG, "onDateSet: date: "+ i + "/" +i1 + "/" +i2);
}
};
}
}
Everything seemed to be fine but when I run it, the app crashes after I click on "Select Date" textView with the following (huge) error message:
E/AnimationUtils: RuntimeException for unknown animation name, resouce ID #0x10a0026
E/AnimationUtils: loadAnimation: getConfiguration = {0 1.3 themeSeq = 0 showBtnBg = 0 214mcc7mnc [en_GB,es_ES] ldltr sw360dp w360dp h616dp 480dpi nrml long port finger -keyb/v/h -nav/h appBounds=Rect(0, 0 - 1080, 1920) s.70 mkbd/h}
loadAnimation: getDisplayMetrics = DisplayMetrics{density=3.0, width=1080, height=1920, scaledDensity=3.8999999, xdpi=435.42825, ydpi=431.57477}
loadAnimation: resourceName = android:anim/date_picker_fade_in_material
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.filmmate, PID: 9738
android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class android.widget.DatePicker
Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class android.widget.DatePicker
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:119)
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:109)
at com.example.filmmate.moreUserInfo$1.onClick(moreUserInfo.java:40)
at android.view.View.performClick(View.java:6897)
at android.widget.TextView.performClick(TextView.java:12727)
at android.view.View$PerformClick.run(View.java:26101)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: android.view.InflateException: Binary XML file line #33: Binary XML file line #18: Error inflating class com.android.internal.widget.DialogViewAnimator
Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class com.android.internal.widget.DialogViewAnimator
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:965)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.widget.DatePickerCalendarDelegate.<init>(DatePickerCalendarDelegate.java:114)
at android.widget.DatePicker.createCalendarUIDelegate(DatePicker.java:200)
at android.widget.DatePicker.<init>(DatePicker.java:173)
at android.widget.DatePicker.<init>(DatePicker.java:145)
at android.widget.DatePicker.<init>(DatePicker.java:141)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
E/AndroidRuntime: at android.app.DatePickerDialog.<init>(DatePickerDialog.java:119)
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:109)
at com.example.filmmate.moreUserInfo$1.onClick(moreUserInfo.java:40)
at android.view.View.performClick(View.java:6897)
at android.widget.TextView.performClick(TextView.java:12727)
at android.view.View$PerformClick.run(View.java:26101)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NumberFormatException: For input string: "res/drawable-xxhdpi-v4/ab_solid_dark_holo.9.png"
at java.lang.Integer.parseInt(Integer.java:608)
at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:133)
at android.content.res.TypedArray.getInt(TypedArray.java:375)
at android.view.animation.Animation.<init>(Animation.java:254)
at android.view.animation.AlphaAnimation.<init>(AlphaAnimation.java:40)
at android.view.animation.AnimationUtils.createAnimationFromXml(AnimationUtils.java:170)
at android.view.animation.AnimationUtils.createAnimationFromXml(AnimationUtils.java:145)
at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:110)
at android.widget.ViewAnimator.setInAnimation(ViewAnimator.java:314)
at android.widget.ViewAnimator.<init>(ViewAnimator.java:58)
at com.android.internal.widget.DialogViewAnimator.<init>(DialogViewAnimator.java:38)
... 39 more
I know it's a strage issue or maybe a stupid question but help is really appreciated, thanks.
答案1
得分: 1
以下是您要翻译的内容:
问题出在设置 Widget_Holo_ActionBar_Solid
上。如果您将其删除,就不会出现错误。尝试以以下方式创建自己的样式:
styles.xml:
<style name="DatePickerDialogStyle" parent="ThemeOverlay.AppCompat.Dialog">
<item name="colorAccent">@color/date_picker_dialog_header_background</item> <!--header background-->
<item name="android:windowBackground">@color/date_picker_dialog_background</item> <!--calendar background-->
<item name="android:colorControlActivated">@color/date_picker_dialog_selected_day</item> <!--selected day-->
<item name="android:textColorPrimary">@color/date_picker_dialog_selected_month</item> <!--days of the month-->
<item name="android:textColorSecondary">@color/date_picker_dialog_day_of_week</item> <!--days of the week-->
</style>
colors.xml
<color name="date_picker_dialog_header_background">@color/colorPrimaryDark</color>
<color name="date_picker_dialog_background">#00FFFFFF</color>
<color name="date_picker_dialog_selected_day">@color/colorAccent</color>
<color name="date_picker_dialog_selected_month">#B71C1C</color>
<color name="date_picker_dialog_day_of_week">#611B1B</color>
然后添加创建日期选择器:
DatePickerDialog dialog = new DatePickerDialog(
MainActivity.this,
R.style.DatePickerDialogStyle,
mDateSetListener,
year,
month,
day);
您将 date_picker_dialog_background
设置为 #00FFFFFF
,因此不需要调用:
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
英文:
The problem is with setting Widget_Holo_ActionBar_Solid
. If You delete it You won't get error. Try to create Your own style in this way:
styles.xml:
<style name="DatePickerDialogStyle" parent="ThemeOverlay.AppCompat.Dialog">
<item name="colorAccent">@color/date_picker_dialog_header_background</item> <!--header background-->
<item name="android:windowBackground">@color/date_picker_dialog_background</item> <!--calendar background-->
<item name="android:colorControlActivated">@color/date_picker_dialog_selected_day</item> <!--selected day-->
<item name="android:textColorPrimary">@color/date_picker_dialog_selected_month</item> <!--days of the month-->
<item name="android:textColorSecondary">@color/date_picker_dialog_day_of_week</item> <!--days of the week-->
</style>
colors.xml
<color name="date_picker_dialog_header_background">@color/colorPrimaryDark</color>
<color name="date_picker_dialog_background">#00FFFFFF</color>
<color name="date_picker_dialog_selected_day">@color/colorAccent</color>
<color name="date_picker_dialog_selected_month">#B71C1C</color>
<color name="date_picker_dialog_day_of_week">#611B1B</color>
And then add to create date picker:
DatePickerDialog dialog = new DatePickerDialog(
MainActivity.this,
R.style.DatePickerDialogStyle,
mDateSetListener,
year,
month,
day);
You set date_picker_dialog_background
to #00FFFFFF
You don't have to call:
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
答案2
得分: 0
在Android版本大于29时,你无法仅使用十六进制值来设置color.xml
。
英文:
On Android > 29 you can't set a color.xml
only hex
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论