Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinearLayout cannot be cast to android.widget.Button

Tags:

android

xml

My original layout xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/app"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0px"
android:background="#e4e8ed"
android:orientation="vertical"
android:padding="0px" >

<include
    android:id="@+id/tabBar"
    layout="@layout/tab" />


<Button
    android:id="@+id/nist"
    android:layout_width="301dp"
    android:layout_height="63dp"
    android:text="Generate NIST file" />

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rectangle"
    android:layout_width="276dp"
    android:layout_height="wrap_content"
    android:background="@drawable/rectangle"
    android:layout_gravity="center_horizontal">

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="128dp"
        android:text="Button" />

</LinearLayout>

</LinearLayout>

But if I move the Button beneath the inner LinearLayout my app is just throwing an exception:

LinearLayout cannot be cast to android.widget.Button 

I think this is weird, how can I manage to do so?

EDIT:

This is the way I "import" the button to my java code:

setTabBar(R.layout.horz_scroll_app);
nist = (Button) findViewById(R.id.nist);

The setTabBar method:

public void setTabBar(int id) {

    LayoutInflater inflater = LayoutInflater.from(this);
    scrollView = (MyHorizontalScrollView) inflater.inflate(
            R.layout.horz_scroll_with_list_menu, null);
    setContentView(scrollView);

    menu = inflater.inflate(R.layout.horz_scroll_menu, null);

    app = inflater.inflate(id, null);
    ViewGroup tabBar = (ViewGroup) app.findViewById(R.id.tabBar);

    ListView listView = (ListView) app.findViewById(R.id.list);

    listView = (ListView) menu.findViewById(R.id.list);

    ArrayList<MenuItem> menuItems = getMenuItems();
    listView.setAdapter(new MenuCustomAdapter(this, menuItems));

    btnSlide = (ImageButton) tabBar.findViewById(R.id.BtnSlide);
    btnSlide.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                btnSlide.setImageResource(R.drawable.lincolor);
                break;
            case MotionEvent.ACTION_UP:
                btnSlide.setImageResource(R.drawable.lin);
                break;
            }

            return false;
        }

    });
    btnSlide.setOnClickListener(new ClickListenerForScrolling(scrollView,
            menu));

    final View[] children = new View[] { menu, app };

    int scrollToViewIdx = 1;
    scrollView.initViews(children, scrollToViewIdx,
            new SizeCallbackForMenu(btnSlide));

}

I should also mention that this problem occurs ONLY for the Button element if I add other elements under the inner LinearLayout it works..

like image 455
Tobias Moe Thorstensen Avatar asked Sep 13 '25 18:09

Tobias Moe Thorstensen


2 Answers

Try cleaning and rebuilding the project, or restart Eclipse if that doesn't work.

like image 138
Maragues Avatar answered Sep 15 '25 08:09

Maragues


Experienced the same problem, what helped me out:

Make sure you do not use the default ID for your view-element (togglebutton, editText, etc).

When you add and remove new elements of the same type in your layout they seem to disturb each other.

So, always use an individual ID.

like image 25
KarlchenKanther Avatar answered Sep 15 '25 08:09

KarlchenKanther