看起来问题可能是由于我对list
的延迟加载造成的:
在我的列表中,我可能有pictures, spinners, multispinners,
...
为了提高性能,我在background
中加载了数据源或图片,因此我不必为此停止我的用户界面。
看起来如果我刷新数据源太快,而这些组件正在加载,则会导致错误。
仍在尝试,我会保持更新。
我的RecyclerView充满了适配器。执行某些操作后,我通过单击另一个列表项来更改数据源,并通知适配器,如下所示:
确保我也停止了滚动: (updateDataSource方法只刷新我的适配器中的ArrayList)
private void setupList() {
//questi sono quelli che non hanno dipendenze.
try {
if (interventionActivityAdapter == null) {
interventionActivityAdapter = new InterventionActivityAdapter(InterventoAttivitaFragment.this,
getActivity(),
new IReturnBoolToParent() {
public void onResult(boolean b) {
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
parseGetActivities();
}
},
myNonRealmActivitieAdapterPojos);
mRecyclerView.setAdapter(interventionActivityAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
} else {
//listState = mRecyclerView.getLayoutManager().onSaveInstanceState();
mRecyclerView.stopScroll();
mRecyclerView.stopNestedScroll();
interventionActivityAdapter.updateDataSource(myNonRealmActivitieAdapterPojos);
interventionActivityAdapter.notifyDataSetChanged();
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
//mRecyclerView.getLayoutManager().onRestoreInstanceState(listState);
}
} catch (Exception ex) {
ex.printStackTrace();
Toast.makeText(getActivity(), "Errore durante l'analisi delle attività.", Toast.LENGTH_SHORT).show();
}
pbSyncState.setVisibility(View.GONE);
}
这是我的适配器
public class InterventionActivityAdapter extends RecyclerView.Adapter<InterventionActivityFieldViewHolder> {
private Activity ctx;
private Fragment f;
private ArrayList<InterventionActivityFieldAdapterPojo> interventionActivityFieldAdapterPojos;
private IReturnBoolToParent iReturnBoolToParent;
public InterventionActivityAdapter(Fragment f,
Activity ctx,
IReturnBoolToParent iReturnBoolToParent,
ArrayList<InterventionActivityFieldAdapterPojo> interventionActivityFieldAdapterPojos) {
this.f = f;
this.ctx = ctx;
this.iReturnBoolToParent = iReturnBoolToParent;
this.interventionActivityFieldAdapterPojos = interventionActivityFieldAdapterPojos;
}
public void updateDataSource(ArrayList<InterventionActivityFieldAdapterPojo> interventionActivityFieldAdapterPojos) {
this.interventionActivityFieldAdapterPojos = interventionActivityFieldAdapterPojos;
}
@Override
public int getItemViewType(int position) {
if (interventionActivityFieldAdapterPojos.get(position).getType() == RuntimeHelper.ActivityChildType.PADRE) {
return 1;
} else {
return 2;
}
}
@Override
public InterventionActivityFieldViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (ctx == null) {
ctx = (Activity) parent.getContext();
}
View inflatedView = LayoutInflater.from(ctx)
.inflate(viewType == 1 ? R.layout.item_interventionactivity : R.layout.item_interventionactivity_son, parent, false);
return new InterventionActivityFieldViewHolder(inflatedView, f, ctx, false, iReturnBoolToParent);
}
@Override
public void onBindViewHolder(InterventionActivityFieldViewHolder holder, int position) {
InterventionActivityFieldAdapterPojo interventionActivityFieldAdapterPojo;
interventionActivityFieldAdapterPojo = interventionActivityFieldAdapterPojos.get(position);
holder.initInterventionActivityField(interventionActivityFieldAdapterPojo);
}
@Override
public int getItemCount() {
return interventionActivityFieldAdapterPojos != null ? interventionActivityFieldAdapterPojos.size() : 0;
}
}
我使用自定义viewHolder,但它超过6万个字符,我无法在此处发布。
错误通过notifyDataSetChanged()的调用到达。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.netribe.business.gridwaywfm.netribe, PID: 25935
java.lang.NullPointerException: Attempt to invoke interface method 'long android.widget.Adapter.getItemId(int)' on a null object reference
at android.widget.AdapterView.fireOnSelected(AdapterView.java:931)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920)
at android.widget.AdapterView.access$300(AdapterView.java:55)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:890)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5621)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
我做错了什么?我无法弄清楚......
我注意到只有当触发notifydatasetchanged的项目滚动了一半(或更多)时才会发生崩溃..这可能有帮助
我还注意到,如果我调试代码,应用程序不会崩溃。 如果我不调试,它会随机崩溃。
我尝试调试并更改了很多东西,但问题仍然存在。使其变为appen的最佳方法是更改更新源的项目,然后歇斯底里地向上和向下滑动列表。这将导致每6-7次尝试一次错误..