how to get id in different layout
-
25-09-2019 - |
Question
i have id "@+id/call"
in single_item.xml
when i use findVewById
it (the layout setcontextview(R.layout.main)
) .the app crash .how to fix the error
Solution
You are trying to find a View (R.id.call) that is declared in R.layout.single_item in the layour R.layout.main, so I guess it is throwing a Null Pointer Exception.
You should either declare your "@+id/call" element in your main.xml file, or set the context view to R.layout.single_item
OTHER TIPS
If you want to access a view in another layout (not the active layout), then you can inflate the layout you want to use and access it that way.
Example:
View inflatedView = getLayoutInflater().inflate(R.layout.other_layout, null);
TextView text = (TextView) inflatedView.findViewById(R.id.text_view);
text.setText("Hello!");
More information about inflating layouts can be found here.
The simple way to fetch id
from a view is:
String id = getResources().getResourceEntryName(view.getId());
View parent = (View)view.getParent();
??? = (???)parent.findViewById(R.id.call);
try this. hope it help