我正在使用改造API。请检查以下屏幕截图:对话框中有类别和子类别列表。单击“确定”按钮后,我必须获取主要类别ID,子类别ID和子类别价格。我使用了sparseBooleanArray。我可以获取main_category id,但是在尝试获取sub_category_id时出现空指针异常。请帮忙。 以下附加代码:
MainAdapter:
holder.select_category.setTag(position);
holder.select_category.setChecked(mSparseBooleanArray.get(position));
holder.select_category.setOnCheckedChangeListener(mCheckedChangeListener);
sublist = new ArrayList<>();
sublist=chat_list.get(position).getSubCategory();
subcategoryAdapter = new SubCategoryAdapter(context, sublist);
holder.recyclerView.setAdapter(subcategoryAdapter);
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
holder.category.setText(chat_list.get(position).getMainCategoryName());
}
CompoundButton.OnCheckedChangeListener mCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
}
};
public ArrayList<String> getCheckedItems() {
ArrayList<String> mTempArry = new ArrayList<String>();
for(int i=0;i<chat_list.size();i++) {
if(mSparseBooleanArray.get(i)) {
mTempArry.add(chat_list.get(i).getMainCategoryId());
//mTempArry.add(sublist.get(i).getSubCategoryId());
Log.e("MAINID",chat_list.get(i).getMainCategoryId());
}
}
return mTempArry;
}
SubAdapter:
public void onBindViewHolder(final DataObjectHolder holder, final int position) {
holder.subcategory.setText(sublist.get(position).getSubCategoryName());
Log.e("Sub Cat Name",sublist.get(position).getSubCategoryName());
holder.subcat.setTag(position);
holder.subcat.setChecked(mSparseBooleanArray.get(position));
holder.subcat.setOnCheckedChangeListener(mCheckedChangeListener);
cost_per_hr=holder.price.getText().toString();
}
CompoundButton.OnCheckedChangeListener mCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
}
};
public ArrayList<String> getSubCheckedItems() {
ArrayList<String> mTempArry = new ArrayList<String>();
for(int i=0;i<sublist.size();i++) {
if(mSparseBooleanArray.get(i)) {
Log.e("TAG","INSIDE SUB");
mTempArry.add(sublist.get(i).getSubCategoryId());
mTempArry.add(cost_per_hr);
Log.e("SUBID",sublist.get(i).getSubCategoryId()+"::"+cost_per_hr);
}
}
return mTempArry;
}
@Override
public int getItemCount() {
return sublist.size();
}
活动:
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
category.dismiss();
ArrayList<String> selectedItems = categoryAdapter.getCheckedItems();
try {
try {
for (int i = 0; i <= selectedItems.size(); i++) {
Log.e("Main", selectedItems.get(i));
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
ArrayList<String> selectedItems_one = subCategoryAdapter.getSubCheckedItems();
try {
for (int i = 0; i <= selectedItems_one.size(); i++) {
Log.e("Sub", selectedItems_one.size()+"");
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
});
单击提交按钮时,我得到了getSubcheckedItems()方法的nullpointer异常。
错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList com.app.qn.adapter.SubCategoryAdapter.getSubCheckedItems()' on a null object reference
at com.app.qn.activity.EditProfileActivity$3.onClick(EditProfileActivity.java:303)
at android.view.View.performClick(View.java:5675)
at android.view.View$PerformClick.run(View.java:22651)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:208)
at android.app.ActivityThread.main(ActivityThread.java:6267)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
2019-04-17 17:48:13.793 19440-21355/com.app.qn E/NativeCrypto: ssl=0x774a444680 cert_verify_callback x509_store_ctx=0x772d7e8a28 arg=0x0
2019-04-17 17:48:13.793 19440-21355/com.app.qn E/NativeCrypto: ssl=0x774a444680 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
答案 0 :(得分:1)
当您选择“ Designer”以外的其他选项时,我想您将获得空指针异常异常 这是因为,从您的屏幕快照中,我只能看到Designer选项具有子类别,并且您已根据项目进行了初始化。
对于除“设计器”之外的其他选项,您没有任何子类别,并且子类别适配器未初始化。
这导致SELECT coalesce(w.words[array_length(w.words, 1) - 2] || ' ', '')
|| coalesce(w.words[array_length(w.words, 1) - 1] || ' ', '')
|| coalesce(w.words[array_length(w.words, 1)], '')
FROM mytable t
CROSS JOIN LATERAL (SELECT regexp_split_to_array(t."name", ' ') words) w;
您的空指针表示您的子适配器为空。
要处理nullpointerexception,请首先检查您的子类别适配器为null还是不进行其他操作。
当然,单击提交按钮时,null pointer exception
对象没有被初始化。确保适配器初始化在范围内。