gpt4 book ai didi

java - 尽管为 api 11 及更高版本设置了 list ,但 Android api 级别 14 上的 GridView 崩溃

转载 作者:行者123 更新时间:2023-11-30 02:36:09 27 4
gpt4 key购买 nike

我正在测试我的应用程序。它在我的 Samsung Galaxy S5 和我的 LG with android 4.1.2 上运行良好。但是当我尝试在 LG 4.0.4(真实设备)或 Nexus 5 的模拟器上测试它时,它由于以下堆栈跟踪而崩溃。我的 list 设置为最小 sdk 11。在堆栈跟踪下方,我还显示了我的 GridView 代码。感谢您提供解决此问题的任何帮助。

堆栈跟踪

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:1028)
at android.view.View.measure(View.java:12603)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
at android.view.View.measure(View.java:12603)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12603)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12603)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12603)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2072)
at android.view.View.measure(View.java:12603)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1044)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

GridView

除了通常的 findViewById 之外,包含 GridView 的 Activity 中唯一特定的代码是

myGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@Override
public void onGlobalLayout() {
if (myGridViewAdapter.getNumColumns() == 0) {
final int numColumns = 2;
if (numColumns > 0) {
final int columnWidth = myGridView.getWidth() / numColumns;// - mImageThumbSpacing;
myGridViewAdapter.setNumColumns(numColumns);
myGridViewAdapter.setItemHeight(columnWidth);
if (BuildConfig.DEBUG) {
Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
}
if (Utils.hasJellyBean()) {
myGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
myGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
}
}
});

然后在方法 getView 的适配器内部,我有这个 fragment

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

// First check if this is the top row
if (position < mNumColumns) {
if (convertView == null) {
convertView = new View(mContext);
}
// Set empty view with height of ActionBar
convertView
.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, mActionBarHeight));
return convertView;
}

最佳答案

尝试

convertView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, mActionBarHeight));

关于java - 尽管为 api 11 及更高版本设置了 list ,但 Android api 级别 14 上的 GridView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26498316/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com