I am trying to access an object of a custom View
created in button1
from button2
this way:
final Button button1 = (Button) findViewById(R.id.generate);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ObjectView object = new ObjectView(getApplicationContext());
object.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
object.setId(componentsIds);
relativeLayout.addView(object);
}
});
final Button button2 = (Button) findViewById(R.id.useComponent);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ObjectView object = findViewById(componentsIds);
object.bringToFront();
}
});
But I get this error:
java.lang.ClassCastException: android.widget.Button cannot be cast to startup.project.views.ObjectView
You have added the custom view to relativeLayout. So, you have to use relativeLayout
to get that custom view.
Try using
ObjectView object = relativeLayout.findViewById(componentsIds);
Instead of
ObjectView object = findViewById(componentsIds);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.