Строка двоичного XML-файла #2: Ошибка надувания класса android.support.v7.widget.CardView в Android<21 в Android Studio

Следующий код работает хорошо в Android больше 21 (леденец),

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_course_not_bought, parent, false);

    return new ViewHolder(v);
}

но в андроиде < 21 вылетает...

Я использую android studio и импортирую библиотеку CardView

11-20 13:39:43.418 5851-5851 E/AndroidRuntime: FATAL EXCEPTION: main
                                           android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.CardView
                                               at android.view.LayoutInflater.createView(LayoutInflater.java:613)
                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                               at ir.dpsoft.lms.RecyclerViewAdapter.RecyclerNotBoughtCourses.onCreateViewHolder(RecyclerNotBoughtCourses.java:55)
                                               at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6488)
                                               at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5675)
                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5558)
                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5554)
                                               at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
                                               at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
                                               at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
                                               at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:583)
                                               at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3694)
                                               at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3110)
                                               at android.view.View.measure(View.java:15518)
                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                               at android.view.View.measure(View.java:15518)
                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                               at android.view.View.measure(View.java:15518)
                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                               at android.view.View.measure(View.java:15518)
                                               at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1217)
                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                               at android.widget.ScrollView.onMeasure(ScrollView.java:321)
                                               at android.view.View.measure(View.java:15518)
                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                               at android.view.View.measure(View.java:15518)
                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                               at android.view.View.measure(View.java:15518)
                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                               at android.view.View.measure(View.java:15518)
                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                               at android.view.View.measure(View.java:15518)
                                               at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1060)

1 ответ

Добавьте эти зависимости в ваш файл Gradle:

    compile 'com.android.support:appcompat-v7:27.0.0'
    compile 'com.android.support:cardview-v7:27.0.0'

Обратитесь к этой документации о совместимости в более ранних версиях:

https://developer.android.com/training/material/compatibility.html

Другие вопросы по тегам