MergeAdapter ClassCastException只有Genymotion

时间:2015-05-11 22:38:51

标签: android android-layout classcastexception genymotion commonsware-cwac

我正在获得一个只在我使用genymotion时发生的类强制转换异常。每当我在真实设备上尝试这个时,我都没有问题。

Stacktrace:

5-11 19:05:10.217    1278-1278/com.vast.homestory.betadebug E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
        at android.widget.ListView.setupChild(ListView.java:1874)
        at android.widget.ListView.makeAndAddView(ListView.java:1843)
        at android.widget.ListView.fillDown(ListView.java:675)
        at android.widget.ListView.fillFromTop(ListView.java:736)
        at android.widget.ListView.layoutChildren(ListView.java:1655)
        at android.widget.AbsListView.onLayout(AbsListView.java:2012)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
        at android.view.Choreographer.doCallbacks(Choreographer.java:562)
        at android.view.Choreographer.doFrame(Choreographer.java:532)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

我正在使用Commonsware的合并适配器和代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    ViewGroup v = (ViewGroup)  inflater.inflate(R.layout.fragment_profile_merge_list, container, false);

    initViews(inflater, container, v);

    setViews();

    setListAdapter(mergeAdapter);

    return v;
}


    public void initViews(LayoutInflater inflater, ViewGroup container, View v) {

    progressBar = (ProgressBar) v.findViewById(R.id.profile_fragment_progress_bar);


    profileHeaderRL = (RelativeLayout) inflater.inflate(R.layout.profile_header_layout, container, false);
    contactInfoRL = (RelativeLayout) inflater.inflate(R.layout.profile_contact_info_layout, container, false);
    addressRL = (RelativeLayout) inflater.inflate(R.layout.profile_address_layout, container, false);
    listingInfoRL = (RelativeLayout) inflater.inflate(R.layout.profile_listing_info_layout, container, false);
    logoutRL = (RelativeLayout) inflater.inflate(R.layout.profile_logout_layout, container, false);

    switch (profileMode) {
        case AGENT_MODE:
            mergeAdapter.addView(profileHeaderRL);
            mergeAdapter.addView(contactInfoRL);
            mergeAdapter.addView(addressRL);
            mergeAdapter.addView(logoutRL);

            break;
        case SEARCH_MODE:
            mergeAdapter.addView(profileHeaderRL);
            mergeAdapter.addView(contactInfoRL);
            mergeAdapter.addView(addressRL);

            break;
        case LISTING_MODE:
            mergeAdapter.addView(profileHeaderRL);
            mergeAdapter.addView(addressRL);
            mergeAdapter.addView(listingInfoRL);
            break;
    }



    profileIV = (RoundedImageView) profileHeaderRL.findViewById(R.id.profile_image_view);
    emailIV = (ImageView) contactInfoRL.findViewById(R.id.profile_email_icon);
    phoneIV = (ImageView) contactInfoRL.findViewById(R.id.profile_phone_icon);
    initialsTV = (TextView) profileHeaderRL.findViewById(R.id.profile_initials_tv);
    nameTV = (TextView) profileHeaderRL.findViewById(R.id.profile_name_tv);
    brokerageTV = (TextView) profileHeaderRL.findViewById(R.id.profile_brokerage_tv);

    officeRL = (RelativeLayout) addressRL.findViewById(R.id.profile_office_rv);

    emailAddressTV = (TextView) contactInfoRL.findViewById(R.id.profile_email_tv);
    phoneNumberTV = (TextView) contactInfoRL.findViewById(R.id.profile_phone_number_tv);
    addressTV = (TextView) addressRL.findViewById(R.id.profile_address_tv);
    cityStateTV = (TextView) addressRL.findViewById(R.id.profile_city_state_tv);
    zipTV = (TextView) addressRL.findViewById(R.id.profile_zip_tv);
    officeTV = (TextView) addressRL.findViewById(R.id.profile_office_tv);
    doneTV = (TextView) profileHeaderRL.findViewById(R.id.profile_done_tv);
    editTV = (TextView) profileHeaderRL.findViewById(R.id.profile_edit_tv);
    logoutButton = (Button) logoutRL.findViewById(R.id.profile_logout_button);

    priceTV = (TextView) listingInfoRL.findViewById(R.id.profile_listing_info_price_tv);
    priceSqftTV = (TextView) listingInfoRL.findViewById(R.id.profile_listing_info_pricesqft_tv);
    bedsTV = (TextView) listingInfoRL.findViewById(R.id.profile_listing_info_beds_tv);
    bathsTV = (TextView) listingInfoRL.findViewById(R.id.profile_listing_info_baths_tv);
    sqftTV = (TextView) listingInfoRL.findViewById(R.id.profile_listing_info_sqft_tv);
    lotSizeTV = (TextView) listingInfoRL.findViewById(R.id.profile_listing_info_lot_size_tv);
    yearTV = (TextView) listingInfoRL.findViewById(R.id.profile_listing_info_years_tv);
    floorsTV = (TextView) listingInfoRL.findViewById(R.id.profile_listing_info_stories_tv);

}

public void setViews() {

    switch (profileMode) {
        case AGENT_MODE:
            setAgentViews();
            setEmailAndPhoneActions();
            break;

        case SEARCH_MODE:
            setSearchViews();
            setEmailAndPhoneActions();
            break;

        case LISTING_MODE:
            setListingViews();
            break;
    }

    nameTV.setText(hsProfileData.getFirstName() + " " + hsProfileData.getLastName());
    initialsTV.setText(ProfileDataFormatter.getInitials(hsProfileData.getFirstName() + " " + hsProfileData.getLastName()));

    HomeStoryApplication.getInstance().getPicasso(getActivity()).
            load(HSApiController.getInstance().getClientProfileImageURL(profileId))
            .fit()
            .centerCrop()
            .into(profileIV, new Callback.EmptyCallback() {
                public void onSuccess() {
                    initialsTV.setVisibility(View.GONE);
                }
            });

    doneTV.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getActivity().onBackPressed();
        }
    });

    editTV.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (profileMode) {
                case AGENT_MODE:
                    Intent editAgentProfileIntent = new Intent(getActivity(), AddNewSearchActivity.class);
                    editAgentProfileIntent.putExtra(PROFILE_ID, profileId);
                    editAgentProfileIntent.putExtra(PROFILE_MODE, AGENT_MODE);
                    startActivity(editAgentProfileIntent);
                    break;
                case SEARCH_MODE:
                    Intent editSearchIntent = new Intent(getActivity(), AddNewSearchActivity.class);
                    editSearchIntent.putExtra(PROFILE_ID, profileId);
                    editSearchIntent.putExtra(PROFILE_MODE, SEARCH_MODE);
                    startActivity(editSearchIntent);
                    break;
                case LISTING_MODE:
                    Intent editListingIntent = new Intent(getActivity(), AddNewListingActivity.class);
                    editListingIntent.putExtra(AddNewListingActivity.EXTRA_PROFILE_ID, listing.getProfileID());
                    startActivity(editListingIntent);
                    break;
            }
        }
    });

}

更新:Commonsware向我展示了如何解决这个问题。我需要传入ListView作为我添加到合并适配器的视图的容器。然后将代码移到onViewCreated,

以下是更新后的代码:

    public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    ListView listView = getListView();

    initViews(listView, view);

    setViews();

    setListAdapter(mergeAdapter);
}

现在initViews只从主机活动中获取LayoutInflater。

    public void initViews(ViewGroup container, View v) {

    LayoutInflater inflater = getActivity().getLayoutInflater();
....

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

initViews()中,您需要将inflate()来电的第二个参数设为ListView。这不是你所拥有的 - 你正在传递放置这个片段的容器。更改initViews()以在其ListView来电中使用inflate(),您应该会有更好的结果。

虽然说实话,如果我对你的代码的解释是正确的,我会期待你在任何Android环境中崩溃,而不仅仅是Genymotion模拟器。