Reputation: 8981
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..
Upvotes: 4
Views: 9008
Reputation: 1
To be further extensive for all of those that may have also missed something obvious in their code (as I did):
Make sure that your java code doesn't literally cast your layout as a button:
public class MainActivity extends AppCompatActivity {
protected Button LL_vertical; //MISTAKE HERE
public void someFunction() {
//R.id.LL_vertical is a linearlayout
LL_vertical = view.findViewById(R.id.LL_vertical);
...
}
... rest of code ..
}
should be
public class MainActivity extends AppCompatActivity {
protected LinearLayout LL_vertical; //MISTAKE FIXED
public void someFunction() {
//R.id.LL_vertical is a linearlayout
LL_vertical = view.findViewById(R.id.LL_vertical);
...
}
... rest of code ..
}
Upvotes: -1
Reputation: 81
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.
Upvotes: 1
Reputation: 38324
Try cleaning and rebuilding the project, or restart Eclipse if that doesn't work.
Upvotes: 11