当前位置: 代码迷 >> java >> java.lang.RuntimeException:无法调用回收的实例! 类型数组navadapter
  详细解决方案

java.lang.RuntimeException:无法调用回收的实例! 类型数组navadapter

热度:52   发布时间:2023-08-04 09:26:10.0
FATAL EXCEPTION: main
Process: com.virinchi.docopd, PID: 19149
java.lang.RuntimeException: Cannot make calls to a recycled instance!
    at android.content.res.TypedArray.getResourceId(TypedArray.java:694)
    at com.virinchi.docopd.adapter.NavDrawerListAdapter.getView(NavDrawerListAdapter.java:67)
    at android.widget.AbsListView.obtainView(AbsListView.java:2347)
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
    at android.widget.ListView.onMeasure(ListView.java:1182)
    at android.view.View.measure(View.java:17565)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
    at android.view.View.measure(View.java:17565)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
    at android.view.View.measure(View.java:17565)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1083)

我也遇到了TypeArray的相同问题。 .recycle( )之后,我们无法添加到对象,因此我删除了回收站。

先前的代码...

TypedArray drawerIconsItems = getResources().obtainTypedArray(
                                       R.array.drawer_menu_icons);
drawerIconsItems.recycle();         
int drawerItemSize = drawerItemsText.length;
for (int size = 0; size < drawerItemSize; size++) {
    drawerCollection.add(new DrawerItems(drawerItemsText[size], 
                      drawerIconsItems.getResourceId(size, -1)));

}

当前代码:

TypedArray drawerIconsItems = getResources().obtainTypedArray(
                                R.array.drawer_menu_icons);

   **//drawerIconsItems.recycle(); //removed after recycle you 
                                    cant use the object latst android version.  
                                    Faced issue in Galaxy S6 Android 6.0.1**

    int drawerItemSize = drawerItemsText.length;
    for (int size = 0; size < drawerItemSize; size++) {
        drawerCollection.add(new DrawerItems(drawerItemsText[size],
                            drawerIconsItems.getResourceId(size, -1)));
    }

您为什么不尝试在recycle()后执行recycle() 我用这个:

private ArrayList<Integer> getGetArrayListFromTypedArray(int arrayId) {
    TypedArray arr = getResources().obtainTypedArray(arrayId);
    ArrayList<Integer> integerArrayList = new ArrayList<>();

    for (int i = 0; i<arr.length(); i++ ){
        integerArrayList.add(i, arr.getResourceId(i, -1));
    }

    arr.recycle();

    return integerArrayList;
}
  相关解决方案