![](/img/trans.png)
[英]how to fix aused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView cannot be cast to android.widget.Button
[英]Android Java AppCompatTextView cannot be cast to LinearLayout
嘗試將多個文本視圖添加到網格視圖單元格時遇到了一些問題。 文本視圖的數量是動態的,它取決於數據庫中的數據。 以下是我為網格視圖設置適配器的方法:
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);
我用於網格視圖的 .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)
任何想法為什么會這樣?
某些單元格只需要一個文本視圖。 但其他一些人可能需要 2 到 3 個更多的文本視圖。
您可以檢查simple_list_item_1.xml
並查看它的根是否為TextView
。 如果是,它將根據您的示例拋出ClassCastException
。
創建一個 xml 布局(例如 cell_layout.xml)並按照您希望單元格的外觀進行設置。 使用 LinearLayout 並在其中放置兩個 TextView。 給出兩個 TextViews id(例如 TextView1、TextView2)。 創建 ArrayAdapter 時,請使用:new ArrayAdapter<>(this, R.layout.cell_layout)。
//Add text for each cell to a string array
String[] cell0 = new String[] {"Test", "Hi"};
//Add all cells to an another string array
String[][] cells = new String[][] {cell0};
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View cellView = convertView;
if(cellView != null) {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
cellView = layoutInflater.inflate(R.layout.scores, parent);
}
cellView.findViewById(R.id.textView1).setText(cells[position][0]);
cellView.findViewById(R.id.textView2).setText(cells[position][1]);
return cellView;
}
//This is a shortcut. Normally you would create a custom ArrayAdapter and a custom class
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.