在API 23及以下版本的回收站视图中添加图像时崩溃

时间:2018-09-28 12:51:06

标签: java android android-recyclerview

我正在通过给ImageView一个位图来动态地尝试将图像添加到回收器视图中(我还尝试将其转换为可绘制对象)。

当我使用更新后的列表调用适配器时,出现以下堆栈跟踪信息:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.xxxxxxx.xxxxxxx.xxxxxxx, PID: 3945
              android.view.InflateException: Binary XML file line #50: Binary XML file line #50: Error inflating class ImageView
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                  at com.xxxxxxx.xxxxxxx.xxxxxxx.FieldNotes.FieldNoteViewAdapter.onCreateViewHolder(FieldNoteViewAdapter.java:53)
                  at com.xxxxxxx.xxxxxxx.xxxxxxx.FieldNotes.FieldNoteViewAdapter.onCreateViewHolder(FieldNoteViewAdapter.java:34)
                  at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6685)
                  at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
                  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
                  at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
                  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
                  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
                  at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
                  at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3225)
                  at android.view.View.measure(View.java:18788)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                  at android.view.View.measure(View.java:18788)
                  at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1283)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                  at android.widget.ScrollView.onMeasure(ScrollView.java:340)
                  at android.view.View.measure(View.java:18788)
                  at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1676)
                  at android.view.View.measure(View.java:18788)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                  at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
                  at android.view.View.measure(View.java:18788)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                  at android.view.View.measure(View.java:18788)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                  at android.view.View.measure(View.java:18788)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                  at android.view.View.measure(View.java:18788)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                  at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
                  at android.view.View.measure(View.java:18788)
                  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
                  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                at android.view.Choreographer$Call

当我在API 24及更高版本上设置图像视图时,这似乎工作得很好。

1 个答案:

答案 0 :(得分:0)

我必须从以下位置更改适配器中的LayoutInflater

View v = LayoutInflater.from(parent.getContext())

收件人:

View v = LayoutInflater.from(context.getApplicationContext())