android.widget.LinearLayout 不能转换为 android.widget.TextView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10869197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:17:40  来源:igfitidea点击:

android.widget.LinearLayout cannot be cast to android.widget.TextView

androidandroid-layoutandroid-listview

提问by AMH

I add a ListViewon runtime like this:

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);

and then I add a item click listener like this:

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

 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();
          }
     });
 }

But then I have an error:

但后来我有一个错误:

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

at this line:

在这一行:

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

Any idea how to get the text in this issue? the adapter looks like this:

知道如何获取此问题中的文本吗?适配器如下所示:

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;
}

and R.layout.shortmenueis simple, only a TextViewlike below:

并且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>

回答by Luksprog

Your row is a TextViewwrapped by a LinearLayoutso you might want to do this:

您的行由 aTextView包裹,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();

回答by sagits

If you are running on a similar problem but you sure you have targeted a linearLayout:

如果你遇到了类似的问题,但你确定你的目标是一个线性布局:

Deletethe file gen/your.app.package/R.java.

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

This happens because of a xml bug, when you delete R.java it will be recreated on the next build/run.

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

回答by RealNmae

I just added android:id="@+id/my_layout" to LinearLayout that wrapped TextView and that solved similar problem.

我刚刚将 android:id="@+id/my_layout" 添加到包装 TextView 的 LinearLayout 并解决了类似的问题。

回答by thrashzone

I had the same problem and I just renamed inserted view object.

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

Like this:

像这样:

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

回答by CoolMind

In my case an XML contained a TextView, but I made a mistake writing

就我而言,XML 包含一个 TextView,但我写错了

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

instead of

代替

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

回答by Naveen rana

You can resolve this by replacing

您可以通过替换来解决此问题

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

with

TextView temp= (TextView) view.findViewById(R.id.textView);

This works for me.

这对我有用。