[英]Android studio app crashes on the button click
I am working on android studio.我正在 android 工作室工作。 I have created some
fragments
and they are smoothly working.我创建了一些
fragments
,它们工作顺利。 The app would be collecting data from users for order booking.该应用程序将从用户那里收集数据以进行订单预订。 Now, I have a button and on this button click, I am calling another layout to enter the products.
现在,我有一个按钮,点击这个按钮,我正在调用另一个布局来输入产品。 The data is selected from the json file.
数据选自 json 文件。 Below is my code
下面是我的代码
Fragment分段
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();
});
Now when I click the add button for the first time it does show the layout of the product but when I click on the same button for the second time the app crashes.现在,当我第一次单击添加按钮时,它确实显示了产品的布局,但是当我第二次单击同一个按钮时,应用程序崩溃了。 Below is the working
下面是工作
Error Message错误信息
The specified child already has a parent.
指定的孩子已经有一个父母。 You must call removeView() on the child's parent first.
您必须首先在孩子的父母上调用 removeView()。
Solution Tried已尝试的解决方案
Inside add layout function I have done the following在里面添加布局 function 我做了以下
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);
}
The product form is shown every time when I click on the Add new Product
button.每次单击“
Add new Product
”按钮时,都会显示产品表单。 But I can't select a product in this case.但在这种情况下,我不能 select 产品。
Update 1更新 1
I have taken addProduct
button in this.initElements(view);
我在
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);
}
Error Log错误日志
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)
The error comes at below part错误出现在以下部分
private void addLayout() {
mLinearLayout.addView(layout2);
}
Update 2更新 2
I have removed the second layout part from onCreatView
and done the following我已经从
onCreatView
中删除了第二个布局部分并完成了以下操作
private void addLayout() {
layout2 = LayoutInflater.from(mContext).inflate(R.layout.product_layout, mLinearLayout, false);
this.initElementsPrd(layout2);
mLinearLayout.addView(layout2);
}
Below is what I am getting now以下是我现在得到的
The autocomplete is working for the 1st one but for the second or 3rd upto N
its not working.自动完成功能适用于第一个,但对于第二个或第三个直到
N
它不起作用。
Any help would be highly appreciated.任何帮助将不胜感激。
Check this检查这个
Button addProduct;
addProduct = view.findViewById(R.id.buttonId);
You are adding a layout by calling this method.您正在通过调用此方法添加布局。 But autocomplete textview will not work for the newly added layout because you haven't set the adapter to newly added Layout.
但是自动完成 textview 不适用于新添加的布局,因为您尚未将适配器设置为新添加的布局。 You need to call setProd() in addLayout() to bind adapter to autocomplete text view when new layout is added.
添加新布局时,您需要在 addLayout() 中调用 setProd() 将适配器绑定到自动完成文本视图。
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);
setProd();
}
Note: For handling dynamic layouts it would be better to handle view object in a list and handle instead of reinstantiating every time to existing object.
注意:为了处理动态布局,最好在列表中处理视图 object 并进行处理,而不是每次都重新实例化现有的 object。
Following the Gowtham KK answer I have done the following changes in my code按照Gowtham KK的回答,我对代码进行了以下更改
private void addLayout() {
layout2 = LayoutInflater.from(mContext).inflate(R.layout.product_layout, mLinearLayout, false);
productAuto = (AutoCompleteTextView) layout2.findViewById(R.id.tv_product);
qtyEditText = (EditText) layout2.findViewById(R.id.prod_qty);
prodPriceEditText = (EditText)layout2.findViewById(R.id.prod_price);
prodSpecsEditText = (EditText)layout2.findViewById(R.id.prod_specs);
removeProduct = (Button)layout2.findViewById(R.id.btn_rmv);
setProd();
}
Then in my setProd()
function然后在我的
setProd()
function
prodArrayList = new ArrayList<String>();
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");
}
prodAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, prodArrayList);
productAuto.setAdapter(prodAdapter);
Implementing the above solution my program is working now实施上述解决方案我的程序现在正在运行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.