【问题标题】:MergeAdapter ClassCastException only w/ GenymotionMergeAdapter ClassCastException 仅 w/ Genymotion
【发布时间】:2015-05-11 22:38:51
【问题描述】:

我得到一个类转换异常,该异常仅在我使用 genymotion 时发生。每当我在真实设备上尝试时,我都没有问题。

堆栈跟踪:

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();
....

感谢您的帮助!

【问题讨论】:

  • 请发布整个堆栈跟踪。
  • @CommonsWare 提供完整的堆栈跟踪

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


【解决方案1】:

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

老实说,如果我对您的代码的解释是正确的,我预计您会在任何 Android 环境中崩溃,而不仅仅是 Genymotion 模拟器。

【讨论】:

  • 它在我的 Nexus4 上运行良好。但是在使用 ListView 作为容器然后将代码移动到 onViewCreated 之后,一切正常,即使在 Genymotion 上也是如此。感谢您的帮助!
猜你喜欢
  • 2015-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
相关资源
最近更新 更多