This is my code:
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.x / 3;
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) gridview.getLayoutParams();
int rows = (int) Math.ceil(mAreas.size()/3.0);
mParams.height = rows * height;
gridview.setLayoutParams(mParams);
Its working fine in any device, but i tried it on a alcatel tablet and for some reason i am getting this error, any suggestion?, it looks like alcatel changed the class for some odd reason and stopped working like the rest of the android distributions?
FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
at android.widget.GridView.onMeasure(GridView.java:1043)
at android.view.View.measure(View.java:15784)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:15784)
at android.widget.ListView.setupChild(ListView.java:1927)
at android.widget.ListView.makeAndAddView(ListView.java:1842)
at android.widget.ListView.fillDown(ListView.java:678)
at android.widget.ListView.fillSpecific(ListView.java:1339)
at android.widget.ListView.layoutChildren(ListView.java:1675)
at android.widget.AbsListView.onLayout(AbsListView.java:2053)
at android.view.View.layout(View.java:14252)
at android.view.ViewGroup.layout(ViewGroup.java:4490)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
at android.view.View.layout(View.java:14252)
at android.view.ViewGroup.layout(ViewGroup.java:4490)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14252)
at android.view.ViewGroup.layout(ViewGroup.java:4490)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
at android.view.View.layout(View.java:14252)
at android.view.ViewGroup.layout(ViewGroup.java:4490)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14252)
at android.view.ViewGroup.layout(ViewGroup.java:4490)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2231)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1995)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1181)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4943)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
at android.view.Choreographer.doCallbacks(Choreographer.java:579)
at android.view.Choreographer.doFrame(Choreographer.java:548)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5413)
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:834)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white">
<LinearLayout
android:id="@+id/time_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/time"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="34sp"
android:layout_marginTop="@dimen/irrigation_text_vertical_margin"
android:layout_marginStart="@dimen/irrigation_text_horizontal_margin"
android:layout_marginLeft="@dimen/irrigation_text_horizontal_margin"
android:text="@string/placeholder"
android:textColor="@color/accent"/>
<TextView
android:id="@+id/status"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="@dimen/irrigation_text_vertical_margin"
android:layout_marginStart="@dimen/irrigation_text_horizontal_margin"
android:layout_marginLeft="@dimen/irrigation_text_horizontal_margin"
android:textColor="@color/accent"/>
</LinearLayout>
<LinearLayout
android:id="@+id/duration_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/time_layout">
<TextView
android:textColor="@color/accent"
android:id="@+id/duration"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginStart="@dimen/irrigation_text_horizontal_margin"
android:layout_marginLeft="@dimen/irrigation_text_horizontal_margin"
android:text="@string/placeholder"/>
<SeekBar
android:id="@+id/duration_seek"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="20" />
</LinearLayout>
<LinearLayout
android:id="@+id/week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/duration_layout"
android:layout_marginTop="10dp"
android:layout_margin="5dp">
<TextView
android:textColor="@color/accent"
android:id="@+id/m"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_circle_true"
android:gravity="center"
android:layout_margin="5dp"
android:text="@string/monday_short"/>
<TextView
android:textColor="@color/accent"
android:id="@+id/t"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_circle_true"
android:gravity="center"
android:layout_margin="5dp"
android:text="@string/tuesday_short"/>
<TextView
android:textColor="@color/accent"
android:id="@+id/w"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_circle_true"
android:gravity="center"
android:layout_margin="5dp"
android:text="@string/wednesday_short"/>
<TextView
android:textColor="@color/accent"
android:id="@+id/th"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_circle_true"
android:gravity="center"
android:layout_margin="5dp"
android:text="@string/thursday_short"/>
<TextView
android:textColor="@color/accent"
android:id="@+id/f"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_circle_true"
android:gravity="center"
android:layout_margin="5dp"
android:text="@string/friday_short"/>
<TextView
android:textColor="@color/accent"
android:id="@+id/s"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_circle_true"
android:gravity="center"
android:layout_margin="5dp"
android:text="@string/saturday_short"/>
<TextView
android:textColor="@color/accent"
android:id="@+id/su"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_circle_true"
android:gravity="center"
android:layout_margin="5dp"
android:text="@string/sunday_short"/>
</LinearLayout>
<Button
android:layout_below="@+id/week"
android:id="@+id/area_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Manage areas"
style="?android:attr/buttonBarButtonStyle"
android:drawableLeft="@drawable/ic_expand"
android:drawableStart="@drawable/ic_expand"
android:textColor="@color/primary_dark"/>
<ImageButton
android:contentDescription="@string/delete_irrigation_cd"
android:id="@+id/trash"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/week"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/btn_trash"
android:scaleType="fitCenter"
style="?android:attr/buttonBarButtonStyle"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"/>
<GridView
android:visibility="gone"
android:paddingTop="1dp"
android:paddingBottom="1dp"
android:background="#DBEEFB"
android:layout_below="@+id/trash"
android:id="@+id/area_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:stretchMode="columnWidth"
android:gravity="center"
android:horizontalSpacing="2dp"
android:verticalSpacing="1dp"
android:scrollbars="none"/>
</RelativeLayout>
Managed to fix it, problem was not inside the code i posted, it was in a library class i was using after removing it, it started working fine for every device thanks for all the input tho!
答案 0 :(得分:0)
错误是因为您想将LayoutParams转换为错误的类型,请尝试:
AbsListView.LayoutParams mParams = (AbsListView.LayoutParams) gridview.getLayoutParams();
答案 1 :(得分:0)
管理修复它,问题不在我发布的代码中,它是在我删除它后使用的库类中,它开始适用于每个设备感谢所有的输入!