我在尝试运行应用程序时意外遇到以下错误:
Binary XML file line #8: Error inflating class android.support.v7.widget.CardView
以下是日志猫:
11-09 13:11:58.558: E/AndroidRuntime(12542): FATAL EXCEPTION: main
11-09 13:11:58.558: E/AndroidRuntime(12542): android.view.InflateException: Binary XML file line #8: Error inflating class android.support.v7.widget.CardView
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-09 13:11:58.558: E/AndroidRuntime(12542): at com.dooba.beta.ThirdFragment.onCreateView(ThirdFragment.java:15)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.os.Handler.handleCallback(Handler.java:615)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.os.Looper.loop(Looper.java:137)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-09 13:11:58.558: E/AndroidRuntime(12542): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 13:11:58.558: E/AndroidRuntime(12542): at java.lang.reflect.Method.invoke(Method.java:511)
11-09 13:11:58.558: E/AndroidRuntime(12542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-09 13:11:58.558: E/AndroidRuntime(12542): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-09 13:11:58.558: E/AndroidRuntime(12542): at dalvik.system.NativeStart.main(Native Method)
11-09 13:11:58.558: E/AndroidRuntime(12542): Caused by: java.lang.ClassNotFoundException: android.support.v7.widget.CardView
11-09 13:11:58.558: E/AndroidRuntime(12542): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
11-09 13:11:58.558: E/AndroidRuntime(12542): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-09 13:11:58.558: E/AndroidRuntime(12542): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
11-09 13:11:58.558: E/AndroidRuntime(12542): ... 19 more
以下是java代码:
public class ThirdFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_city, container, false);
return rootView;
}
}
以下是布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
>
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
提前致谢
答案 0 :(得分:35)
我通过在app / build.gradle
上添加更新的cardview和appcompat解决了这个问题dependencies {
...
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
}
然后重建项目
答案 1 :(得分:4)
您需要指定对v7 cardview library
的依赖关系编辑build.gradle并指定&#34; com.android.support:cardview-v7:21.0。+&#34;在依赖项部分。
答案 2 :(得分:4)
此错误是由于您可能正在使用AndroidX但正在使用此依赖项引起的:
implementation 'com.android.support:cardview-v7:27.1.1'
更改为AndroidX CardView依赖项
implementation 'androidx.cardview:cardview:1.0.0'
并将您的班级更改为布局:
<androidx.cardview.widget.CardView
答案 3 :(得分:2)
只需在xml文件中找到您的cardview布局,例如
<android.support.v7.widget.CardView......</android.support.v7.widget.CardView>
并替换为
<androidx.cardview.widget.CardView.......</androidx.cardview.widget.CardView>
答案 4 :(得分:1)
解决此问题。 首先,您必须从
添加cardview这适合我。
答案 5 :(得分:1)
上面的答案没有帮助。添加
后工作正常@objc private var _stateKVO: Any { return self.state }
var state: SomeNonObjCThing {
willSet { self.willChangeValue(for: \.stateKVO) }
didSet { self.didChangeValue(for: \.stateKVO) }
}
// you can now use #keyPath(_stateKVO) in keyPathsForValuesAffecting...
无法解释。
答案 6 :(得分:1)
使用androidX
也可能导致此问题
尝试将其更改为<androidx.cardview.widget.CardView
,然后崩溃就可以了。
一个提示,检查是否在预览中看到布局。
在gradle中应该像:
implementation 'androidx.cardview:cardview:1.0.0'
答案 7 :(得分:0)
答案 8 :(得分:0)
在检查所有依赖项时,只需检查相关的JAVA类文件,即扩展AppCompatACtivity 。 当然这对你有帮助。
答案 9 :(得分:0)
如果此错误在AndroidX中发生,则需要使用以下依赖项来解决此错误:
implementation 'androidx.cardview:cardview:1.0.0'
答案 10 :(得分:0)
您可能会收到此错误,因为您可能已经从网页中复制了代码并将其粘贴到项目中,尤其是在布局中。请不要这样做,如果这样做,应该看设计部分是否正常。我也遇到了相同的错误,并由我自己解决。