我已经在我的应用程序中进行了recyclelerview,并且在滚动时它会崩溃,而不是在任何特定的用例下。它只会随机崩溃。下面是我的适配器和查看持有人代码。
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);
}
}
这是输出:
这是应用程序崩溃后我遇到的错误
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)
请帮忙。感谢。
答案 0 :(得分:2)
由于可扩展列表视图和调用文本监视器而发生此崩溃。 并为父项和子项使用多个Text Watcher。 你甚至可以改变Radio Group。
尝试使用其他内容更改文本观察器
答案 1 :(得分:0)
Heyy,应用程序因可扩展视图中使用的分隔符而崩溃..我从可扩展列表中删除了分隔符并添加了自定义视图而不是分隔符
<ExpandableListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/layout_SelectAll"
android:background="@color/colorWhite"
android:divider="@null"
android:groupIndicator="@null"
android:paddingBottom="70dp"
/>
问题解决了:))