android.widget.LinearLayout无法强制转换为android.widget.TextView

时间:2012-06-03 09:27:15

标签: android android-layout android-listview

我在运行时添加ListView,如下所示:

     MainMenue =  getResources().getStringArray(R.array.Unit);
    // remove all controls 
    LinearLayout formLayout = (LinearLayout)findViewById(R.id.submenue);
    formLayout.removeAllViews();
    menueview = new ListView(getApplicationContext());               
    menueview.setVisibility(ListView.VISIBLE);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
               LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.RIGHT;
    menueview.setLayoutParams(params);
    menueview.setAdapter(new submenueadapter(menueview.getContext(), MainMenue));
    // Set the on Item 
    SetMenueOnClick() ;
    formLayout.addView(menueview);

然后我添加一个项目点击监听器,如下所示:

 public void SetMenueOnClick() {
     menueview.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {
              final String text = (String) ((TextView)view).getText();
          }
     });
 }

但后来我有一个错误:

06-03 10:59:25.862: E/AndroidRuntime(14732):    at android.view.ViewRoot.handleMessage(ViewRoot.java:2109)
android.widget.LinearLayout cannot be cast to android.widget.TextView

在这一行:

final String text = (String) ((TextView)view).getText();

知道如何获取此问题的文字吗?适配器看起来像这样:

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.shortmenue, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.contents);

    textView.setText(values[position]);

    // Change icon based on name
    String s = values[position];

    System.out.println(s);

    rowView.setBackgroundResource(R.drawable.alternate_list_color);
    return rowView;
}

R.layout.shortmenue很简单,只有TextView如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

                <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/contents"
                android:textSize="34dp" 

                />

</LinearLayout>

6 个答案:

答案 0 :(得分:19)

您的行是由TextView包裹的LinearLayout,因此您可能希望这样做:

LinearLayout ll = (LinearLayout) view; // get the parent layout view
TextView tv = (TextView) ll.findViewById(R.id.contents); // get the child text view
final String text = tv.getText().toString();

答案 1 :(得分:9)

如果您遇到类似问题,但确定已定位到linearLayout:

删除文件 gen / your.app.package / R.java。

这是因为xml错误,当您删除R.java时,它将在下一次构建/运行时重新创建。

答案 2 :(得分:1)

我刚添加了android:id =&#34; @ + id / my_layout&#34;包含TextView并解决了类似问题的LinearLayout。

答案 3 :(得分:0)

我遇到了同样的问题,我只是重命名了插入的视图对象。

像这样:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/newTextViewName"
    android:textSize="34dp"
/>

答案 4 :(得分:0)

在我的例子中,XML包含TextView,但我写错了

final TextView text = (TextView) inflater.inflate(R.layout.item_text, layout);

而不是

final TextView text = (TextView) inflater.inflate(R.layout.item_text, layout, false);

答案 5 :(得分:0)

您可以通过替换

来解决此问题
TextView temp= (TextView) view.findViewById(R.id.textView);

id

这适合我。