Android 7& 8 Only - ClassCastException:LayoutParams无法强制转换为AbsListView $ LayoutParams

时间:2018-03-06 17:56:05

标签: android listview baseadapter recycler-adapter layoutparams

我有一个非常奇怪的问题,我正在调查。我没有编写原始代码,但我现在正在调查它为什么会突然崩溃。看了之后,我没有看到任何崩溃的原因。此外,它在较低的操作系统级别(例如,Android 6)中完美运行;然而,它在Android 7和8中运行时一直崩溃。最初也很难重现,因为崩溃只在第6个索引之后出现...最初认为它可能是一些损坏的数据,因为只有问题表现为某些用户。但是,一旦我提高了列表中的项目数量,我就能够始终如一地重现。当我们遍历药物对象列表并构建它们的列表以便快速参考用户时,我们的列表适配器中发生了类强制转换异常。指数0到5没有问题;但是,在处理索引6之后,应用程序在返回View(最初传入的convertView)时崩溃。我已经通过最多6种药物来完成代码并且从未发现问题。在使用第6个索引(第7个药物)时,当getView(...)方法返回时,它会一致地崩溃。就像我之前说的那样,这只发生在代码在Android 7和8中运行时。当代码在Android 6上运行时(例如),它可以处理10多种药物等等。甚至测试过有20的用户。想知道是否有人遇到过这个和/或有建议/想法。使用适配器的视图回收器,Android 7和8中的某些内容是否有可能发生变化?只有在药物清单大于屏幕可以显示的情况下才会发生这种情况。它几乎就像回收商在Android 7和8中有一个错误......

这是getView(...)实现:

public View getView(int position, View convertView, ViewGroup parent) {

    Log.i(TAG, position + " | " + convertView);

    ViewHolder holder;

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    // MedicationItem
    MedicationItem medItem_pos = rowItems.get(position);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.med_adjustment_reconciliation_item, parent, false);
        holder = new ViewHolder();
        holder.drugInfo = (TextView) convertView.findViewById(R.id.med_adj_recon_list_item_drug_info_textview);
        holder.drugDosageInfo = (TextView) convertView.findViewById(R.id.med_adj_recon_list_item_drug_dosage_info_textview);

        if (medItem_pos.isTaking()) {
            ((ListView)parent).setItemChecked(position, true);

        } else {
            ((ListView)parent).setItemChecked(position, false);
        }

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // Build Drug Info
    StringBuilder sbDrugInfo = new StringBuilder(medItem_pos.getDrug());
    if (StringUtils.isNotEmpty(medItem_pos.getTradeName()))
        sbDrugInfo.append(String.format(" (%s) ", Html.fromHtml(medItem_pos.getTradeName())));
    if (null != medItem_pos.getDose() && medItem_pos.getDose() > 0 && StringUtils.isNotEmpty(medItem_pos.getMedUnit())) {
        MedUnit medUnit = EnumUtils.lookup(MedUnit.class, medItem_pos.getMedUnit());
        sbDrugInfo.append(String.format("%s %s", medItem_pos.getDose(), (null==medUnit?medItem_pos.getMedUnit():medUnit.getLocaleString())));
    }

    // Build Drug Dosage Info
    StringBuilder sbDrugDosageInfo = new StringBuilder();
    sbDrugDosageInfo.append(defineQuantity(medItem_pos.getQuantity(), medItem_pos.getDosageFormUnit()));

    if (StringUtils.isNotBlank(medItem_pos.getFrequency())){
        MedFrequency medFrequency = EnumUtils.lookup(MedFrequency.class, medItem_pos.getFrequency());
        if (null != medFrequency){
            if (sbDrugDosageInfo.length() > 0)
                sbDrugDosageInfo.append(" | ").append(medFrequency.getLocaleString());
            else
                sbDrugDosageInfo.append(medFrequency.getLocaleString());
        }else{
            sbDrugDosageInfo.append(MedFrequency.OTHER.getLocaleString());
        }
    }

    // display drug information
    holder.drugInfo.setText(sbDrugInfo.toString());
    holder.drugDosageInfo.setText(sbDrugDosageInfo.toString());

    return convertView;
}

...
...

private class ViewHolder {
    TextView drugInfo;
    TextView drugDosageInfo;
}

这是错误:

02-20 21:22:11.923 909-7727/? I/ActivityManager: START u0 {cmp=com.some.co/.modules.medadjustment.MedAdjustmentActivity (has extras)} from uid 10246
02-20 21:22:11.926 689-689/? D/QCOM PowerHAL: LAUNCH HINT: ON
02-20 21:22:11.928 689-689/? D/QCOM PowerHAL: Activity launch hint handled
02-20 21:22:11.936 675-2136/? D/audio_hw_primary: out_write: retry previous failed cal level set
02-20 21:22:11.951 21906-21911/? I/zygote: Do partial code cache collection, code=126KB, data=89KB
02-20 21:22:11.952 21906-21911/? I/zygote: After code cache collection, code=124KB, data=88KB
02-20 21:22:11.952 21906-21911/? I/zygote: Increasing code cache capacity to 512KB
02-20 21:22:12.074 21906-21906/? D/AndroidRuntime: Shutting down VM
02-20 21:22:12.074 21906-21906/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.some.co, PID: 21906
    java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
        at android.widget.ListView.removeUnusedFixedViews(ListView.java:1990)
        at android.widget.ListView.layoutChildren(ListView.java:1851)
        at android.widget.AbsListView.onLayout(AbsListView.java:2164)
        at android.view.View.layout(View.java:19659)
        at android.view.ViewGroup.layout(ViewGroup.java:6075)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:19659)
        at android.view.ViewGroup.layout(ViewGroup.java:6075)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:19659)
        at android.view.ViewGroup.layout(ViewGroup.java:6075)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:19659)
        at android.view.ViewGroup.layout(ViewGroup.java:6075)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:19659)
        at android.view.ViewGroup.layout(ViewGroup.java:6075)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:19659)
        at android.view.ViewGroup.layout(ViewGroup.java:6075)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(Unknown Source:68)
        at android.view.View.layout(View.java:19659)
        at android.view.ViewGroup.layout(ViewGroup.java:6075)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:19659)
        at android.view.ViewGroup.layout(ViewGroup.java:6075)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:19659)
        at android.view.ViewGroup.layout(ViewGroup.java:6075)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:761)
        at android.view.View.layout(View.java:19659)
        at android.view.ViewGroup.layout(ViewGroup.java:6075)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2496)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2212)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
02-20 21:22:12.078 21906-21906/? I/zygote: System.exit called, status: 2
02-20 21:22:12.078 21906-21906/? I/AndroidRuntime: VM exiting with result code 2, cleanup skipped.
02-20 21:22:12.097 909-1399/? W/InputDispatcher: channel '643fc74 com.some.co/com.some.co.modules.welcome.MyActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
02-20 21:22:12.097 909-1399/? E/InputDispatcher: channel '643fc74 com.some.co/com.some.co.modules.welcome.MyActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
02-20 21:22:12.098 909-949/? W/zygote64: kill(-21906, 9) failed: No such process
02-20 21:22:12.098 909-1664/? I/ActivityManager: Process com.some.co (pid 21906) has died: fore TOP 

最后,这是布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/med_adj_recon_list_item_background_frame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/button_white_with_gray_border"
    android:layout_margin="@dimen/spacing_tiny">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="@dimen/spacing_small"
        android:layout_gravity="center_vertical"
        android:layout_margin="@dimen/spacing_small">
        <TextView
            android:id="@+id/med_adj_recon_list_item_drug_info_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/font_normal"
            android:textColor="@drawable/button_custom_med_adjustment_text_color"/>
        <TextView
            android:id="@+id/med_adj_recon_list_item_drug_dosage_info_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/font_small"
            android:textColor="@drawable/button_custom_med_adjustment_text_color"/>
    </LinearLayout>
    <View
        android:id="@+id/med_adj_recon_list_item_divider"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="@drawable/color_selector"/>
    <ImageView
        android:id="@+id/med_adj_recon_list_item_image_view"
        android:layout_width="@dimen/icon_width"
        android:layout_height="@dimen/icon_height"
        style="@style/IconImageViewBase"
        android:src="@drawable/tick_selector"/>
</LinearLayout>

有时另一双眼睛有帮助。非常感谢帮助!

以下是加载ListView的片段Class的导入:

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.some.co.R;
import com.some.co.fragment.CommonFragment;
import com.some.co.modules.home.cards.CardFragment;
import com.some.co.modules.medadjustment.model.DeclineMedicationReason;
import com.some.co.modules.medadjustment.model.MedicationItem;
import com.some.co.modules.medadjustment.model.TextDrawable;

import java.lang.reflect.Type;
import java.util.List;

以下是build.gradle中的依赖项:

final ANDROID_SDK_VERSION = '22.2.1'

dependencies {
    compile "com.android.support:design:$ANDROID_SDK_VERSION"
    compile "com.android.support:appcompat-v7:$ANDROID_SDK_VERSION"
    compile "com.android.support:support-v4:$ANDROID_SDK_VERSION"
    compile "com.android.support:cardview-v7:$ANDROID_SDK_VERSION"
    compile "com.android.support:recyclerview-v7:$ANDROID_SDK_VERSION"
    compile 'com.android.support:multidex:1.0.1'
    // only include the needed dependencies from play-services, 8.4 version above require SDK23.
    compile 'com.google.android.gms:play-services-auth:8.3.0'
    compile 'com.google.android.gms:play-services-base:8.3.0'
    compile 'com.google.android.gms:play-services-gcm:8.3.0'
    compile 'com.google.android.gms:play-services-analytics:8.3.0'
    compile 'com.google.android.gms:play-services-location:8.3.0'
    compile 'org.apache.commons:commons-lang3:3.3.2'
    compile 'org.greenrobot:eventbus:3.0.0'
    compile 'org.greenrobot:greendao-encryption:2.2.2'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'joda-time:joda-time:2.8.2'
    compile 'org.joda:joda-convert:1.4' // for proguard, b/c we're using joda-time (above)
    // for UI testing
    androidTestCompile 'com.jayway.android.robotium:robotium-solo:4.3.1'
    androidTestCompile 'com.squareup.spoon:spoon-client:1.7.0'
}

此外,&#39; compileSdkVersion&#39;和&#39; targetSdkVersion&#39;都设置为22

0 个答案:

没有答案