voy a dar un poco mas de contexto estoy tratando llamar una lista de una tabla alojada en una base de datos de mysql por medio de webservices, y quiero mostrarla en la fragment list, como un array recuperado en la funcion que se llama buscarProducto
public class MyMesasRecyclerViewAdapter extends RecyclerView.Adapter<MyMesasRecyclerViewAdapter.ViewHolder> {
private final List<Mesas> mValues;
private final OnListFragmentInteractionListener mListener;
public MyMesasRecyclerViewAdapter(List<Mesas> items, OnListFragmentInteractionListener listener) {
mValues = items;
mListener = listener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_mesas, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.txtViewNumeroMesa.setText(holder.mItem.getNum_mesa_app());
holder.txtViewMesero.setText(holder.mItem.getNombremes_app());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != mListener) {
mListener.onListFragmentInteraction(holder.mItem);
}
}
});
}
@Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView txtViewMesero;
public final TextView txtViewNumeroMesa;
public Mesas mItem;
public ViewHolder(View view) {
super(view);
mView = view;
txtViewMesero = view.findViewById(R.id.txtMesero);
txtViewNumeroMesa = view.findViewById(R.id.txtNumeroMesa);
}
@Override
public String toString() {
return super.toString() + " '" + txtViewNumeroMesa.getText() +"'";
}
}
}
private void buscarProducto(String URL) {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;
mesasList = new ArrayList<>();
for (int i = 0; i < response.length(); i++) {
try {
jsonObject = response.getJSONObject(i);
mesasList.add(new Mesas(jsonObject.getString("num_mesa"), jsonObject.getString("mesero")));
} catch (JSONException e) {
Toast.makeText(getContext(), "ERROR DE CONEXION", Toast.LENGTH_SHORT).show();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "ERROR DE CONEXION", Toast.LENGTH_SHORT).show();
}
}
);
requestQueue= Volley.newRequestQueue(getContext());
requestQueue.add(jsonArrayRequest);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mesas_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
//Lista de Mesas
buscarProducto("http://localhost/TLKRestaurant/buscarmesas.php");
//Asociacion de el adaptador a el RecyclerView
adapterMesas = new MyMesasRecyclerViewAdapter(mesasList, mListener);
recyclerView.setAdapter(adapterMesas);
}
return view;
}
El logcat manda el siguiente error
2020-06-28 21:16:02.176 13942-13942/com.androidavanzado.tlkcomandas E/AndroidRuntime: FATAL EXCEPTION: main Process: com.androidavanzado.tlkcomandas, PID: 13942 java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at com.androidavanzado.tlkcomandas.MyMesasRecyclerViewAdapter.getItemCount(MyMesasRecyclerViewAdapter.java:53) at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4044) at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3534) at android.view.View.measure(View.java:25448) at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227) at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572) at android.view.View.measure(View.java:25448) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6884) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143) at android.view.View.measure(View.java:25448) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6884) at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:403) at android.view.View.measure(View.java:25448) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6884) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.view.View.measure(View.java:25448) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6884) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552) at android.widget.LinearLayout.measureVertical(LinearLayout.java:842) at android.widget.LinearLayout.onMeasure(LinearLayout.java:721) at android.view.View.measure(View.java:25448) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6884) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at com.android.internal.policy.DecorView.onMeasure(DecorView.java:739) at android.view.View.measure(View.java:25448) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3361) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2203) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2460) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1934) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8127) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972) at android.view.Choreographer.doCallbacks(Choreographer.java:796) at android.view.Choreographer.doFrame(Choreographer.java:731) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7523) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)