我在viewholder patern中使用附加的类来在每次尝试使用woth适配器时实例化并重用我的项目视图
无论何时,只要我试图将视图文本设置为某个值,它就会崩溃,因为视图为空
public class SpinnerServicesAdapter extends ArrayAdapter<MyService> {
private LayoutInflater inflater;
private Context mContext;
private ArrayList<MyService> services;
private String headerTxt;
public SpinnerServicesAdapter(Context mContext, int textViewResourceId, ArrayList services, String headerTxt) {
super(mContext, textViewResourceId, services);
this.mContext = mContext;
this.services = services;
this.headerTxt = headerTxt;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
SpinnerViewHolder spinnerViewHolder;
if (convertView == null) {
if (position == 0) {
convertView = inflater.inflate(R.layout.spinner_header_item_two, parent, false);
} else {
convertView = inflater.inflate(R.layout.spinner_item_two, parent, false);
}
spinnerViewHolder = new SpinnerViewHolder(convertView);
convertView.setTag(spinnerViewHolder);
} else {
spinnerViewHolder = (SpinnerViewHolder) convertView.getTag();
}
if (position == 0) {
spinnerViewHolder.txtVue.setText(headerTxt);
} else {
spinnerViewHolder.txtVue.setText(services.get(position).getName_en());
}
return convertView;
}
class SpinnerViewHolder {
TextView txtVue;
public SpinnerViewHolder(View v) {
txtVue = (TextView) v.findViewById(R.id.spinner_item_two);
}
}
}
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.infovas.hanger.adapters.SpinnerServicesAdapter.getCustomView(SpinnerServicesAdapter.java:60)
at com.infovas.hanger.adapters.SpinnerServicesAdapter.getView(SpinnerServicesAdapter.java:42)
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:197)
at android.widget.Spinner.onMeasure(Spinner.java:507)
at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:416)
at android.view.View.measure(View.java:17548)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1083)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
at android.view.View.measure(View.java:17548)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17548)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:17548)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17548)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17548)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17548)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2632)
at android.view.View.measure(View.java:17548)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2125)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1238)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1455)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1126)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6041)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
at android.view.Choreographer.doCallbacks(Choreographer.java:596)
at android.view.Choreographer.doFrame(Choreographer.java:557)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
答案 0 :(得分:1)
首先,问题是(TextView) v.findViewById(R.id.spinner_item_two)
返回null
(==在R.id.spinner_item_two
中找不到ID为v
的视图。您必须在XML布局中检查此ID是否正确。
其次,你通过位置膨胀不同的视图。问题是框架将不加区别地重用它。您必须覆盖getItemViewType(int position)
才能返回两个int,每个类型的视图一个。
最后,该框架为我们提供了一个更加美观的RecyclerView
方法。如果你有时间,check it。
答案 1 :(得分:1)
您使用一个ViewHolder
用于两个不同的适配器布局,这会产生可为空的视图,或者,您可以覆盖getItemViewType
中的Adapter
方法以确定标头项< / strong>和正常项目像这样:
1-在适配器类中定义这些常量:
private final int TYPE_HEADER = 0;
private final int TYPE_ITEM = 1;
2-覆盖适配器中的getItemViewType(int position)
方法:
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
3-为标题项创建ViewHolder
课程:
class SpinnerHeaderViewHolder {
TextView txtHeader;
public SpinnerHeaderViewHolder(View v) {
txtHeader= (TextView) v.findViewById(R.id.spinner_header_item_text_view);
}
}
不要忘记推荐&#39; txtHeader&#39;到(标题布局)中的正确ID
4-现在在getCustomView(int position, View convertView, @NonNull ViewGroup parent)
方法中,执行以下操作:
public View getCustomView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
final int itemType = getItemViewType(position);
if (convertView == null) {
if (itemType == TYPE_HEADER) {
convertView = inflater.inflate(R.layout.spinner_header_item_two, parent, false);
viewHolder = new SpinnerHeaderViewHolder(convertView);
} else {
convertView = inflater.inflate(R.layout.spinner_item_two, parent, false);
viewHolder = new SpinnerViewHolder(convertView);
}
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (position == 0) {
((SpinnerHeaderViewHolder) viewHolder).txtHeader.setText(headerTxt);
} else {
((SpinnerViewHolder) viewHolder).txtVue.setText(services.get(position).getName_en());
}
return convertView;
}
希望这会有所帮助。
答案 2 :(得分:0)
你确定两个文件“spinner_header_item_two.xml”和“spinner_item_two.xml?
中存在id为”spinner_item_two“的textview