[英]android - how to declare a widget from another layout?
I have a fragment layout that contains a class, but it needs to edit an imageview that is in another layout fragment. 我有一个包含一个类的片段布局,但是它需要编辑另一个布局片段中的imageview。 Example:
例:
public class LayoutFragment extends Fragment {
private ImageView imageView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_layout, container, false);
imageView = view.findViewById(R.id.imageView); // this imageview is not fragment_layout, he is in fragment_layout2
addChild();
return view;
}
private void addChild() {
LayoutInflater inflates = LayoutInflater.from(getContext());
int id = R.layout.dynamic_layout_budget;
LinearLayout relativeLayout = (LinearLayout) inflates.inflate(id, null, false);
imageView.setImageBitmap(bitmap);
layout.addView(relativeLayout);
}
}
but if I do this, an error of type "attempt to invoke virtual method on a null object reference" 但是如果执行此操作,将出现类型为“试图在空对象引用上调用虚拟方法”的错误
How could I manipulate this imageview that is in another fragment layout? 如何处理处于另一个片段布局中的此imageview?
The issue may be because you are paying null as a parent in inflater. 问题可能是因为您为充气机的父母支付了零费用。
So instead of passing null in parent of 因此,与其在父级中传递null
LinearLayout relativeLayout = (LinearLayout) inflates.inflate(id, null, false);
try passing a view in that like 尝试像这样传递视图
LinearLayout relativeLayout = (LinearLayout) inflates.inflate(id, layout, false);
But from your code I'm not sure that what's the value of layout
variable inside add method. 但是从您的代码中,我不确定add方法中的
layout
变量的值是多少。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.