尝试更改ListView单元格高度时,为什么我在模拟器上出现此错误?

时间:2015-11-13 16:12:03

标签: android android-listview

我的以下代码适用于我的API 21设备 但是,它无法在Android Studio中的Nexus 7 API 19仿真器上运行:

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = LayoutInflater.from(getContext());
    View customView = inflater.inflate(R.layout.shopping_list_cell, parent, false);


    customView.setLayoutParams(new RelativeLayout.LayoutParams(cellWidth, cellHeight + 20));
    customView.requestLayout();


    return customView;
}

导致此问题的确切行如下(当我取出这两行时,我在模拟器上没有出现任何错误):

customView.setLayoutParams(new RelativeLayout.LayoutParams(cellWidth, cellHeight + 20));
customView.requestLayout();

这是我的错误消息:

致命的例外:主要                                                                                进程:john.smith.johnsmith.testapplication,PID:1649                                                                                java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法强制转换为android.widget.AbsListView $ LayoutParams                                                                                    在android.widget.ListView.setupChild(ListView.java:1826)                                                                                    在android.widget.ListView.makeAndAddView(ListView.java:1793)                                                                                    在android.widget.ListView.fillDown(ListView.java:691)                                                                                    在android.widget.ListView.fillFromTop(ListView.java:752)                                                                                    在android.widget.ListView.layoutChildren(ListView.java:1630)                                                                                    在android.widget.AbsListView.onLayout(AbsListView.java:2091)                                                                                    在android.view.View.layout(View.java:14817)                                                                                    在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                    在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)                                                                                    在android.view.View.layout(View.java:14817)                                                                                    在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                    在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)                                                                                    在android.widget.FrameLayout.onLayout(FrameLayout.java:388)                                                                                    在android.view.View.layout(View.java:14817)                                                                                    在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                    在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)                                                                                    在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)                                                                                    在android.widget.LinearLayout.onLayout(LinearLayout.java:1434)                                                                                    在android.view.View.layout(View.java:14817)                                                                                    在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                    在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)                                                                                    在android.widget.FrameLayout.onLayout(FrameLayout.java:388)                                                                                    在android.view.View.layout(View.java:14817)                                                                                    在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                    在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)                                                                                    在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)                                                                                    在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)                                                                                    在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5670)                                                                                    在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761)                                                                                    在android.view.Choreographer.doCallbacks(Choreographer.java:574)                                                                                    在android.view.Choreographer.doFrame(Choreographer.java:544)                                                                                    在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747)                                                                                    在android.os.Handler.handleCallback(Handler.java:733)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                    在android.os.Looper.loop(Looper.java:136)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:5017)                                                                                    at java.lang.reflect.Method.invokeNative(Native Method)                                                                                    在java.lang.reflect.Method.invoke(Method.java:515)                                                                                    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)                                                                                    在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:1)

尝试将Relative.LayoutParams更改为AbsListView.LayoutParams

 @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View customView = inflater.inflate(R.layout.shopping_list_cell, parent, false);


        customView.setLayoutParams(new AbsListView.LayoutParams(cellWidth, cellHeight + 20));
       //remove  customView.requestLayout();


        return customView;
    }