Date Picker Android Studio 崩溃 错误膨胀类 android.widget.DatePicker

huangapple go评论122阅读模式
英文:

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):

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;#000000&quot;
tools:context=&quot;.moreUserInfo&quot;&gt;
&lt;TextView
android:id=&quot;@+id/textView10&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;97dp&quot;
android:layout_alignParentStart=&quot;true&quot;
android:layout_alignParentTop=&quot;true&quot;
android:layout_alignParentEnd=&quot;true&quot;
android:layout_marginStart=&quot;0dp&quot;
android:layout_marginTop=&quot;0dp&quot;
android:layout_marginEnd=&quot;0dp&quot;
android:gravity=&quot;center&quot;
android:text=&quot;Enter your birthday&quot;
android:textColor=&quot;#ffffff&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/tvDate&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_alignParentStart=&quot;true&quot;
android:layout_alignParentTop=&quot;true&quot;
android:layout_alignParentEnd=&quot;true&quot;
android:layout_marginStart=&quot;3dp&quot;
android:layout_marginTop=&quot;91dp&quot;
android:layout_marginEnd=&quot;-3dp&quot;
android:gravity=&quot;center&quot;
android:hint=&quot;Select Date&quot;
android:textColor=&quot;#ffffff&quot;
android:textColorHint=&quot;#CEA6FFCC&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;/RelativeLayout&gt;

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 = &quot;moreUserInfo&quot;;
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, &quot;onDateSet: date: &quot;+ i + &quot;/&quot; +i1 + &quot;/&quot; +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.&lt;init&gt;(DatePickerDialog.java:119)
at android.app.DatePickerDialog.&lt;init&gt;(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.&lt;init&gt;(DatePickerCalendarDelegate.java:114)
at android.widget.DatePicker.createCalendarUIDelegate(DatePicker.java:200)
at android.widget.DatePicker.&lt;init&gt;(DatePicker.java:173)
at android.widget.DatePicker.&lt;init&gt;(DatePicker.java:145)
at android.widget.DatePicker.&lt;init&gt;(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.&lt;init&gt;(DatePickerDialog.java:119)
at android.app.DatePickerDialog.&lt;init&gt;(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: &quot;res/drawable-xxhdpi-v4/ab_solid_dark_holo.9.png&quot;
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.&lt;init&gt;(Animation.java:254)
at android.view.animation.AlphaAnimation.&lt;init&gt;(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.&lt;init&gt;(ViewAnimator.java:58)
at com.android.internal.widget.DialogViewAnimator.&lt;init&gt;(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:

&lt;style name=&quot;DatePickerDialogStyle&quot; parent=&quot;ThemeOverlay.AppCompat.Dialog&quot;&gt;
&lt;item name=&quot;colorAccent&quot;&gt;@color/date_picker_dialog_header_background&lt;/item&gt;  &lt;!--header background--&gt;
&lt;item name=&quot;android:windowBackground&quot;&gt;@color/date_picker_dialog_background&lt;/item&gt;     &lt;!--calendar background--&gt;
&lt;item name=&quot;android:colorControlActivated&quot;&gt;@color/date_picker_dialog_selected_day&lt;/item&gt; &lt;!--selected day--&gt;
&lt;item name=&quot;android:textColorPrimary&quot;&gt;@color/date_picker_dialog_selected_month&lt;/item&gt; &lt;!--days of the month--&gt;
&lt;item name=&quot;android:textColorSecondary&quot;&gt;@color/date_picker_dialog_day_of_week&lt;/item&gt;    &lt;!--days of the week--&gt;
&lt;/style&gt;

colors.xml

&lt;color name=&quot;date_picker_dialog_header_background&quot;&gt;@color/colorPrimaryDark&lt;/color&gt;
&lt;color name=&quot;date_picker_dialog_background&quot;&gt;#00FFFFFF&lt;/color&gt;
&lt;color name=&quot;date_picker_dialog_selected_day&quot;&gt;@color/colorAccent&lt;/color&gt;
&lt;color name=&quot;date_picker_dialog_selected_month&quot;&gt;#B71C1C&lt;/color&gt;
&lt;color name=&quot;date_picker_dialog_day_of_week&quot;&gt;#611B1B&lt;/color&gt;

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

huangapple
  • 本文由 发表于 2020年7月31日 03:23:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63179980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定