how to get id in different layout

后端 未结 5 1854
無奈伤痛
無奈伤痛 2020-12-30 14:52

i have id \"@+id/call\" in single_item.xml when i use findVewById it (the layout setcontextview(R.layout.main)) .the ap

相关标签:
5条回答
  • 2020-12-30 15:29

    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

    0 讨论(0)
  • 2020-12-30 15:36

    The simple way to fetch id from a view is:

    String id = getResources().getResourceEntryName(view.getId());
    
    0 讨论(0)
  • 2020-12-30 15:37

    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.

    0 讨论(0)
  • 2020-12-30 15:42

    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.

    0 讨论(0)
  • 2020-12-30 15:55
    View parent = (View)view.getParent();
    ??? = (???)parent.findViewById(R.id.call);
    

    try this. hope it help

    0 讨论(0)
提交回复
热议问题