我正在android studio上工作。我已经创建了一些fragments
,它们工作得很顺利。这个应用程序将从用户那里收集数据用于订单预订。现在,我有一个按钮,点击这个按钮,我调用另一个布局来进入产品。数据是从json文件中选择的。下面是我的代码
片段
Button addProduct;
private LinearLayout mLinearLayout;
public View layout2;
public View view = null;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.new_survey_form_layout, container, false); // my main layout
mLinearLayout = view.findViewById(R.id.ll_prod);// the layout in which I want to call my below layout
layout2 = inflater.inflate(R.layout.product_layout, mLinearLayout, false);// product layout
this.initElements(view);// all of my fields against my new_survey_layout_form_layout
this.initElementsPrd(layout2); // the fields of my second layout
}
}
private void initElementsPrd(View view) {
productAuto = (AutoCompleteTextView) view.findViewById(R.id.tv_product);
qtyEditText = (EditText) view.findViewById(R.id.prod_qty);
prodPriceEditText = (EditText)view.findViewById(R.id.prod_price);
prodSpecsEditText = (EditText)view.findViewById(R.id.prod_specs);
setProd();// to set the adapters for selecting the product name
}
private void setProd()
{
Log.d("Ali", "sub div");
if (prodArrayList == null) {
prodArrayList = new ArrayList<String>();
Log.d("Ali", "prod new");
} else {
prodArrayList.clear();
Log.d("Ali", "prod clear");
}
if (isNewSurvey) {
prodArrayList.addAll(new ProductManager(getActivity()).getAvailableProducts());
Log.d("Ali", "prod is new booking");
} else {
prodArrayList.addAll(new ProductManager(getActivity()).getProducts());
Log.d("Ali", "sub div edit survey");
}
if (prodAdapter == null) {
Log.d("Ali", "sub div new adapter");
prodAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, prodArrayList);
productAuto.setAdapter(prodAdapter);
} else {
Log.d("Ali", "sub div notify");
prodAdapter.notifyDataSetChanged();
}
}
addProduct.setOnClickListener(v -> {
addLayout();
});
现在,当我第一次点击添加按钮时,它确实显示了产品的布局,但当我第二次点击同一按钮时,应用程序崩溃。
错误消息
指定的子系已经有父系。您必须先在子系的父系上呼叫removeView()。
已尝试解决方案
在添加布局功能里面我做了如下
private void addLayout(){
layout2 = LayoutInflater.from(mContext).inflate(R.layout.product_layout, mLinearLayout, false);
productAuto = (AutoCompleteTextView) view.findViewById(R.id.tv_product);
qtyEditText = (EditText) view.findViewById(R.id.prod_qty);
prodPriceEditText = (EditText)view.findViewById(R.id.prod_price);
prodSpecsEditText = (EditText)view.findViewById(R.id.prod_specs);
}
每次单击Add new Product
按钮时都会显示产品窗体。但在这种情况下我无法选择产品。
更新1
我在this.initElements(view);
中使用了addProduct
按钮
private void initElements(View view) {
newSurveyScrollview = (ScrollView) view.findViewById(R.id.new_survey_scrollview);
refNofield1 = (EditText) view.findViewById(R.id.ref_no_field_1);
consumerNameEditText = (EditText) view.findViewById(R.id.consumer_name);
consumerAddressEditText = (EditText) view.findViewById(R.id.consumer_address);
latitudeEditText = (EditText) view.findViewById(R.id.latitude);
longitudeEditText = (EditText) view.findViewById(R.id.longitude);
placeEditText = (EditText) view.findViewById(R.id.place);
subDivisionSpinner = (Spinner) view.findViewById(R.id.sub_division_spinner);
cameraButton = (Button) view.findViewById(R.id.open_camera);
saveButton = (Button) view.findViewById(R.id.save_survey_form);
resetButton = (Button) view.findViewById(R.id.survey_reset);
getRefNoButton = (Button) view.findViewById(R.id.get_ref_no_button);
getLatLangButton = (ImageButton) view.findViewById(R.id.get_lat_lang_button);
addProduct = (Button)view.findViewById(R.id.btn_prd);
}
错误日志
Process: com.example.thumbsol.accuratesurvey, PID: 6854
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:5038)
at android.view.ViewGroup.addView(ViewGroup.java:4869)
at android.view.ViewGroup.addView(ViewGroup.java:4809)
at android.view.ViewGroup.addView(ViewGroup.java:4782)
at com.example.thumbsol.accuratesurvey.fragments.SurveyFormFragment.addLayout(SurveyFormFragment.java:1904)
at com.example.thumbsol.accuratesurvey.fragments.SurveyFormFragment.lambda$initListeners$1$com-example-thumbsol-accuratesurvey-fragments-SurveyFormFragment(SurveyFormFragment.java:550)
at com.example.thumbsol.accuratesurvey.fragments.SurveyFormFragment$$ExternalSyntheticLambda3.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:6608)
at android.view.View.performClickInternal(View.java:6585)
at android.view.View.access$3100(View.java:785)
at android.view.View$PerformClick.run(View.java:25921)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
错误出现在以下部分
private void addLayout() {
mLinearLayout.addView(layout2);
}
更新2
我已经从onCreatView
中删除了第二个布局部分,并执行了以下操作
private void addLayout() {
layout2 = LayoutInflater.from(mContext).inflate(R.layout.product_layout, mLinearLayout, false);
this.initElementsPrd(layout2);
mLinearLayout.addView(layout2);
}
下面是我现在得到的
第一个自动完成功能正常,但第二个或第三个直到N
的自动完成功能不正常。
任何帮助都将不胜感激。
2条答案
按热度按时间k2fxgqgv1#
您正在通过调用此方法添加布局。但自动完成文本视图将不适用于新添加的布局,因为您尚未将适配器设置为新添加的布局。添加新布局时,您需要在addLayout()中调用setProd()以将适配器绑定到自动完成文本视图。
注意:对于处理动态布局,最好在列表中处理视图对象,并处理,而不是每次都重新示例化现有对象。
zz2j4svz2#
根据Gowtham K K的答案,我在代码中做了以下更改
然后在我的
setProd()
函数中实现上述解决方案,我的程序现在正在工作