[英]android.widget.LinearLayout cannot be cast to android.widget.TextView
I add a ListView
on 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.shortmenue
is simple, only a TextView
like 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>
Your row is a TextView
wrapped by a LinearLayout
so you might want to do this: 您的行是由
LinearLayout
包装的TextView
,因此您可能希望这样做:
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();
If you are running on a similar problem but you sure you have targeted a linearLayout: 如果您遇到类似问题,但确定已针对linearLayout:
Delete the 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时,它将在下一次构建/运行时重新创建。
我刚刚将android:id =“@ + id / my_layout”添加到了包装TextView并解决了类似问题的LinearLayout。
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"
/>
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);
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. 这适合我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.