gpt4 book ai didi

java - Android Java AppCompatTextView 无法转换为 LinearLayout

转载 作者:行者123 更新时间:2023-12-02 10:54:50 26 4
gpt4 key购买 nike

我在尝试将多个 TextView 添加到 GridView 单元格时遇到一些问题。 TextView 的数量是动态的,取决于数据库中的数据。以下是我为 GridView 设置适配器的方法:

 adapter = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_list_item_1, items) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = (LinearLayout) super.getView(position, convertView, parent);

// for simplicity I hardcoded two text views

TextView cell = new TextView(this.getContext());
cell.setText("Test");

TextView cell2 = new TextView(this.getContext());
cell2.setText("HI");

ll.addView(cell);
ll.addView(cell2);

return ll;
}
};

gridView.setAdapter(adapter);

我的 GridView .xml 文件:

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="7"
android:requiresFadingEdge="vertical"
android:scrollbars="none"
/>

但是,我收到以下错误消息:

java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.LinearLayout
at com.mainapp.scheduler.InstrumentSchedulerFragment$3.getView(InstrumentSchedulerFragment.java:586)
at android.widget.AbsListView.obtainView(AbsListView.java:2365)
at android.widget.GridView.onMeasure(GridView.java:1065)
at android.view.View.measure(View.java:22071)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1212)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1552)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:724)
at android.view.View.measure(View.java:22071)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2422)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1504)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1761)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

有什么想法为什么会这样吗?

enter image description here

某些单元格仅需要一个 TextView 。但其他一些人可能需要 2 到 3 次以上的 TextView 。

最佳答案

您可以检查simple_list_item_1.xml并查看它的根是否是TextView。如果是,它将根据您的示例抛出 ClassCastException

关于java - Android Java AppCompatTextView 无法转换为 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51851601/

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