android - 如何从另一个布局声明一个小部件? [英] 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. 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?
推荐答案
问题可能是因为您作为父母在充气机中支付了 null.
The issue may be because you are paying null as a parent in inflater.
所以不要在
LinearLayout relativeLayout = (LinearLayout) inflates.inflate(id, null, false);
尝试像这样传递一个视图
try passing a view in that like
LinearLayout relativeLayout = (LinearLayout) inflates.inflate(id, layout, false);
但是从您的代码中,我不确定 add 方法中 layout
变量的值是什么.
But from your code I'm not sure that what's the value of layout
variable inside add method.
这篇关于android - 如何从另一个布局声明一个小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!