0

When I'm clicking a button, that is indeed inside an LinearLayout, I get this error on this line:

LinearLayout ll = (LinearLayout) view;

My method looks like this:

 public void adjDoa(final View view) {

    final CharSequence[] items = {"Get on with it!", "Doable!", "Maybe, maybe not.",      "Unlikely!", "When pigs fly!"};

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(GetItActivity.this);
    dialogBuilder.setTitle("Choose doability!");
    dialogBuilder.setItems(items, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {

            LinearLayout ll = (LinearLayout) view;
            TextView text = (TextView)ll.findViewById(R.id.tViewChDoa);
            text.setText(items[which]);
        }

    });
    dialogBuilder.create().show();
}

What does it mean?

9
  • You have Button and your casting Button to LinearLayout. Commented Sep 18, 2013 at 15:13
  • So what calls the adjDoa method? Commented Sep 18, 2013 at 15:14
  • I think that it means that when the onClick is called, view is a Button, not a LinearLayout, and you cannot cast a Button to LinearLayout Commented Sep 18, 2013 at 15:14
  • 1
    Try this LinearLayout parent = (LinearLayout) view.getParent() Commented Sep 18, 2013 at 15:17
  • The method is called when clicking the button in the linearlayout. It brings up a alertdialog from which you choose one of the five alternatives. But when I click OK after selecting one alternative it crashes. Not sure what to do, it worked when I had scrollview. So what should I use instead of view? Yes, I'm new to this... :) Commented Sep 18, 2013 at 16:35

2 Answers 2

11

Although it can be as easy as sockeqwe's answer. Sometimes this error can happen if you have copied/pasted/moved a part of your layout XML to another part of the XML. If that is the case, cleaning the project through Project -> Clean will resolve that problem.


In this particular case it seems like you want the parent of the button, in which case you need to call view.getParent(); like this: (Here I am typecasting to ViewGroup instead of LinearLayout because it really doesn't matter which kind of layout the parent is)

ViewGroup ll = (ViewGroup) view.getParent();

If you only have one view with the id tViewChDoa, you don't need to get the LinearLayout before you get that view, if you have this code in your activity then this will work:

TextView text = (TextView) findViewById(R.id.tViewChDoa);
Sign up to request clarification or add additional context in comments.

6 Comments

Part of this answer was correct. I removed the LinearLayout ll = (LinearLayout) view; Originally I didn't use that line, but I also didn't have the "final" in public void adjDoa(final View view). So to sum it up, it was the addition of "final" that did it. Thanks.
Maybe I didnt understand your comment above, but what you saying makes absolutely no sence. And you should also avoid to use getParent(), beacuse it breaks a little bit the concept of decoupling view (XML) and functionality (java code). If one day you change your xml layout for this component (insert another parent Layout, or i.e. switch from LinearLayout to RelativeLayout) getParent() will throw a CastException. So at least use View as type instead of LinearLayout: View ll = view.getParent(); However the best solution would be to already pass the correct view as parameter in adjDoa()
@sockeqwe The way I see it is that the view parameter is a button (since this is an onClick method from what I understand). And he wants to get the TextView which is in the same layout. I modified my answer to cast to ViewGroup instead of LinearLayout because you have a good point there. I did also include in my answer that in reality, if there is only one view with the id tViewChDoa you can call findViewById on the activity and you will get the correct view.
@SimonAndréForsberg Allright :D But He could also use View instead of ViewGroup, if the only thing he wants to do is to call findViewById()
It works, because you use now the Activity.findViewById(). Just a tip: I would recommend you to to call findViewById(R.id.tViewChDoa) in your Activity.onCreate(). findViewById() is a expensive operation because it has to traverse (in worst case) all child views in the main layout (hence ListView should use ViewHolder). So if you call (TextView)findViewById(R.id.tViewChDoa); as reaction on a ClickEvent could lead to a bad user experience because pressing / releasing the button could look a little bit laggy. It should not be a big deal in 95 % of your activities, but I just want to mention it
|
2

The View you have passed as parameter in adjDoa(final View view) is a Button and not a LinearLayout!

Therefore it cant be cast to LinearLayout and raises this exception.

Check which parameter you pass to this method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.