forループを介してJava内に新しい行を作成することで動的に埋められるTableLayoutを含むPopupWindowを作成しています。このコードを実行すると、新しい行用に作成した挿入ポイントを指す ClassCastException が返されます。PopupWindow を開く Adapter クラス内のボタンの 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)
どんな助けや指示も大歓迎です!ありがとう!