android.widget.LinearLayout cannot be cast to android.widget.TextView
Asked Answered
S

6

15

I add a ListView on runtime like this:

     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:

<?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>
Stoker answered 3/6, 2012 at 9:27 Comment(0)
G
22

Your row is a TextView wrapped by a LinearLayout so you might want to do this:

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();
Grouchy answered 3/6, 2012 at 9:33 Comment(0)
A
11

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

Delete the file 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.

Anglim answered 22/4, 2014 at 3:21 Comment(2)
Worked! Stupid enough :pVivisectionist
Or just try to hit the REBUILD buttonSum
S
1

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

Skyeskyhigh answered 12/10, 2014 at 5:54 Comment(1)
Strangely enough this solved my list_item.xml problem too, where I had multiple items in it but no id set for the LinearLayout containing them. Somehow android can't distinguish it from a normal View, probably because it doesn't have the R.layout type without the id, so it tries to cast it and fails.Matchwood
H
0

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"
/>
Honey answered 10/11, 2014 at 12:43 Comment(0)
S
0

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

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);
Sylvanite answered 5/10, 2016 at 11:42 Comment(0)
A
0

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.

Alf answered 8/7, 2017 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.