web-dev-qa-db-ja.com

MaterialCardViewがmaterial:1.1.0でクラッシュする

MaterialCardViewをcardviewレイアウトとして使用しています。 Android-studioで、現在のimplementation "com.google.Android.material:material:1.0.0"ではなく1.1.0に更新する必要があることが示されました。

<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:app="http://schemas.Android.com/apk/res-auto"
    Android:id="@+id/edit_name"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:layout_gravity="center"
    Android:orientation="vertical"
    Android:layout_margin="@dimen/activity_vertical_margin">

  <com.google.Android.material.card.MaterialCardView
      Android:layout_width="match_parent"
      Android:layout_height="wrap_content"
      app:cardCornerRadius="8dp"
      app:cardElevation="8dp">

      <...more layout>
</com.google.Android.material.card.MaterialCardView>
</LinearLayout>

1.1.0では、クラッシュしますError inflating class com.google.Android.material.card.MaterialCardView

だから、私は3つのケースがあります:

  1. 1.0.0に固執し、MaterialCardViewを使用します(機能しますが、推奨されません)
  2. 1.1.0(または1.2.0-alpha)に更新し、androidx.CardViewを使用します(機能しますが、推奨されません)
  3. 1.1.0に更新し、MaterialCardViewを変更します(推奨、ただし機能しない

オプション#3で進みたいのですが、解決策がないので、誰かが手掛かりを与えることができますか?

更新私のスタイル(style&style-nightもあります。ここではstyleのみを投稿しています):

<resources>

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.DayNight">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

  <style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
  </style>

  <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

  <style name="ThemeOverlay.AppCompat.navTheme">
    <!-- Color of text and icon when SELECTED -->
    <item name="colorPrimary">#ffffff</item>
    <!-- Background color when SELECTED -->
    <item name="colorControlHighlight">@color/colorPrimary</item>

  </style>

</resources>

そしてここにエラーがあります(from Logcat

2020-02-12 13:34:18.171 12562-12562/com.example.phocast E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.phocast, PID: 12562
    Android.view.InflateException: Binary XML file line #10 in com.example.phocast:layout/fragment_place: Binary XML file line #10 in com.example.phocast:layout/fragment_place: Error inflating class com.google.Android.material.card.MaterialCardView
    Caused by: Android.view.InflateException: Binary XML file line #10 in com.example.phocast:layout/fragment_place: Error inflating class com.google.Android.material.card.MaterialCardView
    Caused by: Java.lang.reflect.InvocationTargetException
        at Java.lang.reflect.Constructor.newInstance0(Native Method)
        at Java.lang.reflect.Constructor.newInstance(Constructor.Java:343)
        at Android.view.LayoutInflater.createView(LayoutInflater.Java:854)
        at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:1006)
        at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:961)
        at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:1123)
        at Android.view.LayoutInflater.rInflateChildren(LayoutInflater.Java:1084)
        at Android.view.LayoutInflater.inflate(LayoutInflater.Java:682)
        at Android.view.LayoutInflater.inflate(LayoutInflater.Java:534)
        at Android.view.LayoutInflater.inflate(LayoutInflater.Java:481)
        at com.example.phocast.MainActivity.onOptionsItemSelected(MainActivity.Java:555)
        at Android.app.Activity.onMenuItemSelected(Activity.Java:4137)
        at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.Java:383)
        at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.Java:219)
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.Java:109)
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.Java:109)
        at androidx.appcompat.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.Java:64)
        at androidx.appcompat.widget.Toolbar$1.onMenuItemClick(Toolbar.Java:207)
        at androidx.appcompat.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.Java:781)
        at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.Java:840)
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.Java:158)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.Java:991)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.Java:981)
        at androidx.appcompat.widget.ActionMenuView.invokeItem(ActionMenuView.Java:625)
        at androidx.appcompat.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.Java:151)
        at Android.view.View.performClick(View.Java:7125)
        at Android.view.View.performClickInternal(View.Java:7102)
        at Android.view.View.access$3500(View.Java:801)
        at Android.view.View$PerformClick.run(View.Java:27336)
        at Android.os.Handler.handleCallback(Handler.Java:883)
        at Android.os.Handler.dispatchMessage(Handler.Java:100)
        at Android.os.Looper.loop(Looper.Java:214)
        at Android.app.ActivityThread.main(ActivityThread.Java:7356)
        at Java.lang.reflect.Method.invoke(Native Method)
        at com.Android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.Java:492)
        at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:930)
     Caused by: Java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
        at com.google.Android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.Java:248)
        at com.google.Android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.Java:222)
        at com.google.Android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.Java:150)
        at com.google.Android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.Java:81)
        at com.google.Android.material.card.MaterialCardView.<init>(MaterialCardView.Java:124)
        at com.google.Android.material.card.MaterialCardView.<init>(MaterialCardView.Java:114)
        at Java.lang.reflect.Constructor.newInstance0(Native Method) 
        at Java.lang.reflect.Constructor.newInstance(Constructor.Java:343) 
        at Android.view.LayoutInflater.createView(LayoutInflater.Java:854) 
        at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:1006) 
        at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:961) 
        at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:1123) 
        at Android.view.LayoutInflater.rInflateChildren(LayoutInflater.Java:1084) 
        at Android.view.LayoutInflater.inflate(LayoutInflater.Java:682) 
        at Android.view.LayoutInflater.inflate(LayoutInflater.Java:534) 
        at Android.view.LayoutInflater.inflate(LayoutInflater.Java:481) 
        at com.example.phocast.MainActivity.onOptionsItemSelected(MainActivity.Java:555) 
        at Android.app.Activity.onMenuItemSelected(Activity.Java:4137) 
        at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.Java:383) 
        at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.Java:219) 
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.Java:109) 
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.Java:109) 
        at androidx.appcompat.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.Java:64) 
        at androidx.appcompat.widget.Toolbar$1.onMenuItemClick(Toolbar.Java:207) 
        at androidx.appcompat.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.Java:781) 
        at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.Java:840) 
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.Java:158) 
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.Java:991) 
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.Java:981) 
        at androidx.appcompat.widget.ActionMenuView.invokeItem(ActionMenuView.Java:625) 
        at androidx.appcompat.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.Java:151) 
        at Android.view.View.performClick(View.Java:7125) 
        at Android.view.View.performClickInternal(View.Java:7102) 
        at Android.view.View.access$3500(View.Java:801) 
        at Android.view.View$PerformClick.run(View.Java:27336) 
        at Android.os.Handler.handleCallback(Handler.Java:883) 
        at Android.os.Handler.dispatchMessage(Handler.Java:100) 
        at Android.os.Looper.loop(Looper.Java:214) 
        at Android.app.ActivityThread.main(ActivityThread.Java:7356) 
        at Java.lang.reflect.Method.invoke(Native Method) 
        at com.Android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.Java:492) 
        at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:930) 
2020-02-12 13:34:18.620 12562-12584/com.example.phocast I/example.phocas: Background young concurrent copying GC freed 110384(4259KB) AllocSpace objects, 46(1212KB) LOS objects, 41% free, 7792KB/12MB, paused 1.092ms total 150.548ms
2020-02-12 13:34:18.692 12562-12562/com.example.phocast I/Process: Sending signal. PID: 12562 SIG: 9
13
BaRud

styles.xmlでテーマをTheme.MaterialComponentsに変更しても機能しない場合は、styleMaterialCardのタグ。

style="@style/Widget.MaterialComponents.CardView"
0
Neeraj Sewani