我正在创建一个包含 TableLayout 的 PopupWindow,它通过 for 循环在我的 Java 中创建新行来动态填充。当我运行此代码时,我得到一个 ClassCastException,它指向我为新行创建的插入点。这是 Adapter 类中打开 PopupWindow 的按钮的 onClick 代码:
Button quickView = (Button) convertView.findViewById(R.id.openpopup);
quickView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//create new popupWindow with popuptable as its layout
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = null;
View popupView = inflater.inflate(R.layout.popuptable, null);
if (scores[0] == null ) {
pw = new PopupWindow(popupView, 100, 100, true);
TextView score = (TextView) popupView.findViewById(R.id.popupmessage);
score.setText("Error!");
} else {
pw = new PopupWindow(popupView, 200, 200, true); //wrap content?
for (int i = 0; i < z; i++) {
//create a new row here
View rowView = inflater.inflate(R.layout.table_row, null); //this is the template for each view
//add info to the row
TextView tag = (TextView) rowView.findViewById(R.id.tag); //id within the layout
tag.setText(tags[i]);
//insert the new row into the popuptable view
View insertPoint = (TextView) popupView.findViewById(R.id.popuptitle);
((ViewGroup) insertPoint).addView(rowView, i, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
}
pw.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
});
ClassCastException 指向该行View insertPoint = (TextView) popupView.findViewById(R.id.popuptitle);
。
popupView
使用此处的布局 popuptable.xml 进行膨胀:
<TableLayout
xmlns:custom="http://schemas.android.com/apk/res/com.tforan.blobtag4"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fitScoreTable"
android:shrinkColumns="0"
android:layout_marginTop="5dp" >
<TextView
android:id="@+id/popuptitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textIsSelectable="true"
android:text="Tags"
android:textColor="#000000"
android:textSize="14dp" />
<TextView
android:id="@+id/popupmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textIsSelectable="true"
android:textColor="#000000"
android:textSize="14dp" />
</TableLayout>
最后,logcat 显示以下错误:
09-13 13:26:57.426: E/AndroidRuntime(2594): FATAL EXCEPTION: main
09-13 13:26:57.426: E/AndroidRuntime(2594): java.lang.ClassCastException: android.widget.TextView
09-13 13:26:57.426: E/AndroidRuntime(2594): at com.tforan.blobtag4.ResultsAdapter$1.onClick(ResultsAdapter.java:125)
09-13 13:26:57.426: E/AndroidRuntime(2594): at android.view.View.performClick(View.java:2533)
09-13 13:26:57.426: E/AndroidRuntime(2594): at android.view.View$PerformClick.run(View.java:9299)
09-13 13:26:57.426: E/AndroidRuntime(2594): at android.os.Handler.handleCallback(Handler.java:587)
09-13 13:26:57.426: E/AndroidRuntime(2594): at android.os.Handler.dispatchMessage(Handler.java:92)
09-13 13:26:57.426: E/AndroidRuntime(2594): at android.os.Looper.loop(Looper.java:150)
09-13 13:26:57.426: E/AndroidRuntime(2594): at android.app.ActivityThread.main(ActivityThread.java:4358)
09-13 13:26:57.426: E/AndroidRuntime(2594): at java.lang.reflect.Method.invokeNative(Native Method)
09-13 13:26:57.426: E/AndroidRuntime(2594): at java.lang.reflect.Method.invoke(Method.java:507)
09-13 13:26:57.426: E/AndroidRuntime(2594): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
09-13 13:26:57.426: E/AndroidRuntime(2594): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
09-13 13:26:57.426: E/AndroidRuntime(2594): at dalvik.system.NativeStart.main(Native Method)
非常感谢任何帮助或指导!谢谢!