gpt4 book ai didi

java - 滚动可扩展 ListView 时应用程序随机崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 09:35:25 24 4
gpt4 key购买 nike

我一直在我的应用程序中使用 recyclerview 并在滚动时崩溃,而不是在任何特定用例下..它只是随机崩溃。下面是我的适配器和 View Holder 代码。

class CustomListView_Base_Adapter extends BaseExpandableListAdapter {

Context context;
private LayoutInflater inflater = null;

public CustomListView_Base_Adapter(Context context) {
this.inflater = LayoutInflater.from(context);
this.context = context;

}

@Override
public int getGroupCount() {
return dataforSubcategoryItem.size();
}

@Override
public int getChildrenCount(int groupPosition) {
return 1;
}

@Override
public Object getGroup(int groupPosition) {
return dataforSubcategoryItem.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return dataforSubcategoryItem.get(groupPosition);
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View resultView = convertView;
final ViewHolder holder;

if (resultView == null) {
resultView = inflater.inflate(R.layout.add_subcategory_list_item, null); //TODO change layout id
holder = new ViewHolder(resultView);
resultView.setTag(holder);
} else {
holder = (ViewHolder) resultView.getTag();
}
final SubcategoryItemBean beanObj = (SubcategoryItemBean) getGroup(groupPosition);
holder.tvColumnTitle.setText(beanObj.getName());
holder.checkBox.setOnCheckedChangeListener(null);


CustomWatcher oldWatcher = (CustomWatcher) holder.etInput.getTag();
if (oldWatcher != null)
holder.etInput.removeTextChangedListener(oldWatcher);

if (beanObj.isColumnSelected()) {
holder.checkBox.setChecked(true);
holder.etInput.setText(dataforSubcategoryItem.get(groupPosition).getLabel());
holder.etInput.setEnabled(true);
listView.expandGroup(groupPosition);
} else {
holder.checkBox.setChecked(false);
holder.etInput.setText("");
holder.etInput.setEnabled(false);
listView.collapseGroup(groupPosition);
}


CustomWatcher newWatcher = new CustomWatcher(groupPosition, holder.etInput);
holder.etInput.setTag(newWatcher);
holder.etInput.addTextChangedListener(newWatcher);
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
dataforSubcategoryItem.get(groupPosition).setColumnSelected(true);
} else {
dataforSubcategoryItem.get(groupPosition).setColumnSelected(false);

}
notifyDataSetChanged();
}
});


return resultView;
}

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View resultView = convertView;
final ViewHolder holder;


if (resultView == null) {
resultView = inflater.inflate(R.layout.child_list_item, null); //TODO change layout id
holder = new ViewHolder(resultView, context);
resultView.setTag(holder);
} else {
holder = (ViewHolder) resultView.getTag();
}
final SubcategoryItemBean beanObj = (SubcategoryItemBean) getChild(groupPosition, childPosition);
CustomWatcher oldWatcher = (CustomWatcher) holder.defaultValues.getTag();
if (oldWatcher != null)
holder.defaultValues.removeTextChangedListener(oldWatcher);
holder.radiogroup.setOnCheckedChangeListener(null);

holder.defaultValues.setText(beanObj.getValue());
if (beanObj.getInput_type().equalsIgnoreCase(getString(R.string.column_type_text))) {
holder.defaultValues.setInputType(InputType.TYPE_CLASS_TEXT);
holder.defaultValues.setHint("Enter Default Value");
holder.rbText.setChecked(true);
} else if (beanObj.getInput_type().equalsIgnoreCase(getString(R.string.column_type_number))) {
holder.defaultValues.setInputType(InputType.TYPE_CLASS_NUMBER);
holder.defaultValues.setHint("Enter Default Value");
holder.rbNumber.setChecked(true);
} else if (beanObj.getInput_type().equalsIgnoreCase(getString(R.string.column_type_radiobutton))) {
holder.defaultValues.setInputType(InputType.TYPE_CLASS_TEXT);
holder.defaultValues.setHint("Enter comma separated values");
holder.rbRadioButton.setChecked(true);
} else if (beanObj.getInput_type().equalsIgnoreCase(getString(R.string.column_type_checkbox))) {
holder.rbCheckbox.setChecked(true);
holder.defaultValues.setHint("Enter comma separated values");

holder.defaultValues.setInputType(InputType.TYPE_CLASS_TEXT);
} else {
holder.rbDropdown.setChecked(true);
holder.defaultValues.setHint("Enter comma separated values");
holder.defaultValues.setInputType(InputType.TYPE_CLASS_TEXT);
}


holder.radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton button = (RadioButton) group.findViewById(group.getCheckedRadioButtonId());
dataforSubcategoryItem.get(groupPosition).setInput_type(button.getText().toString());
holder.defaultValues.setText("");
notifyDataSetChanged();

}
});

CustomWatcher newWatcher = new CustomWatcher(groupPosition, holder.defaultValues);
holder.defaultValues.setTag(newWatcher);
holder.defaultValues.addTextChangedListener(newWatcher);
return resultView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}

private class CustomWatcher implements TextWatcher {
private int position;
private EditText editText;

private CustomWatcher(int position, EditText editText) {
this.position = position;
this.editText = editText;
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void afterTextChanged(Editable editable) {
if (editText.getId() == R.id.addCategory_chooseItem_EditText) {
dataforSubcategoryItem.get(position).setLabel(editable.toString().trim());
System.out.println("CustomWatcher.onTextChanged label" + editable.toString());
}
if (editText.getId() == R.id.defaultValues_addSubCategory_chooseItem) {
dataforSubcategoryItem.get(position).setValue(editable.toString().trim());
System.out.println("CustomWatcher.afterTextChanged default value " + editable.toString());
}

}
}
}


class ViewHolder {

CheckBox checkBox;
TextView tvColumnTitle;
EditText etInput;
RadioGroup radiogroup;
RadioButton rbText, rbNumber, rbRadioButton, rbCheckbox, rbDropdown;
EditText defaultValues;
LinearLayout columnDetailsLayout;

public ViewHolder(View view) {
checkBox = (CheckBox) view.findViewById(R.id.addCategory_chooseItem_Checkbox);
tvColumnTitle = (TextView) view.findViewById(R.id.addCategory_chooseItem_TextView);
etInput = (EditText) view.findViewById(R.id.addCategory_chooseItem_EditText);
}

public ViewHolder(View view, Context context) {
columnDetailsLayout = (LinearLayout) view.findViewById(R.id.columnDetailsLayout);
radiogroup = (RadioGroup) view.findViewById(R.id.radiogroup_addSubCategory_chooseItem);
rbText = (RadioButton) view.findViewById(R.id.rbText_addSubCategory_chooseItem);
rbNumber = (RadioButton) view.findViewById(R.id.rbNumber_addSubCategory_chooseItem);
rbRadioButton = (RadioButton) view.findViewById(R.id.rbRadioButton_addSubCategory_chooseItem);
rbCheckbox = (RadioButton) view.findViewById(R.id.rbCheckbox_addSubCategory_chooseItem);
rbDropdown = (RadioButton) view.findViewById(R.id.rbDropdown_addSubCategory_chooseItem);

defaultValues = (EditText) view.findViewById(R.id.defaultValues_addSubCategory_chooseItem);

rbText.setChecked(true);
}


}

这是输出:

enter image description here

这是我在应用程序崩溃后遇到的错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: global.edios.inventorymanagementtabletapp, PID: 2927
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setBounds(android.graphics.Rect)' on a null object reference
at android.widget.ExpandableListView.drawDivider(ExpandableListView.java:536)
at android.widget.ListView.dispatchDraw(ListView.java:3288)
at android.widget.ExpandableListView.dispatchDraw(ExpandableListView.java:353)
at android.view.View.draw(View.java:15234)
at android.widget.AbsListView.draw(AbsListView.java:4110)
at android.view.View.updateDisplayListIfDirty(View.java:14167)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2530)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

请帮忙。谢谢。

最佳答案

此崩溃是由于可扩展 ListView 和调用文本观察器而发生的。并为父项和子项使用多个 Text Watcher。另外,您甚至可以更改 Radio Group。

尝试用其他东西改变文本观察器

关于java - 滚动可扩展 ListView 时应用程序随机崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43657551/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com