首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ClassCastException与TableLayout在PopupWindow中

ClassCastException与TableLayout在PopupWindow中
EN

Stack Overflow用户
提问于 2013-09-13 18:53:42
回答 1查看 403关注 0票数 0

我正在创建一个PopupWindow,其中包含一个TableLayout,该TableLayout通过一个for循环在我的Java中创建新行来动态填充。当我运行这段代码时,我会得到一个ClassCastException,它指向为新行创建的插入点。下面是Adapter类中打开PopupWindow的按钮的PopupWindow代码:

代码语言:javascript
复制
    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 (就在这里)进行膨胀的:

代码语言:javascript
复制
<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显示以下错误:

代码语言:javascript
复制
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)

任何帮助或指导都是非常感谢的!谢谢!

EN

回答 1

Stack Overflow用户

发布于 2013-09-13 19:00:34

在这一行

代码语言:javascript
复制
 View insertPoint = (TextView) popupView.findViewById(R.id.popuptitle);

您使用的是TextView变量View,这是错误的。您需要TextView而不是视图

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18793288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档