how to get id in different layout
i have id "@+id/call"
in single_item.xml
when i use findVewById
it (the layout 开发者_C百科setcontextview(R.layout.main)
) .the app crash .how to fix the error
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.
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
The simple way to fetch id
from a view is:
String id = getResources().getResourceEntryName(view.getId());
If you want to get the id in the fragment use the following method. replace the return statement with view like this.
View view= inflater.inflate(R.layout.fragment_left, container, false);
img=view.findViewById(R.id.image);
return view;
call all the methods before you return the view.
View parent = (View)view.getParent();
??? = (???)parent.findViewById(R.id.call);
try this. hope it help
精彩评论