Llevo desarrollando una app y funciona correcto en lo que necesito. Es un parser de noticias. El problema ocurre cuando quiero verificar la conexión a Internet, con Internet funciona bien.
Pero me desconecto, la abro y se detiene, en el log me muestra:
FATAL EXCEPTION: main Process: com.tucuenca.com.tucuenca3, PID: 30480
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.tucuenca.com.tucuenca3.adaptadorNoticia.getItemCount(adaptadorNoticia.java:74)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2556)
at android.view.View.measure(View.java:17998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:17998)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:846)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:512)
at android.view.View.measure(View.java:17998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:17998)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:940)
at android.view.View.measure(View.java:17998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:17998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
at android.view.View.measure(View.java:17998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:17998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2732)
at android.view.View.measure(View.java:17998)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2412)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1414)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1638)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1292)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6598)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:572)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5717)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Por ahora solo quería que me muestre que tiene o no acceso a Internet.
Con esta función compruebo la conexión a Internet, pero como les comentó solo quiero por ahora que me diga si hay o no conexión.
ConnectivityManager cm;
NetworkInfo ni;
cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
ni = cm.getActiveNetworkInfo();
boolean tipoConexion1 = false;
boolean tipoConexion2 = false;
if (ni != null) {
ConnectivityManager connManager1 = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager1.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
ConnectivityManager connManager2 = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobile = connManager2.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mWifi.isConnected()) {
tipoConexion1 = true;
Toast.makeText(getBaseContext(), "Wifi", Toast.LENGTH_SHORT).show();
}
if (mMobile.isConnected()) {
tipoConexion2 = true;
Toast.makeText(getBaseContext(), "Mobil", Toast.LENGTH_SHORT).show();
}
if (tipoConexion1 == true || tipoConexion2 == true) {
/* Estas conectado a internet usando wifi o redes moviles, puedes enviar tus datos */
Toast.makeText(getBaseContext(), "Conexión a internet ", Toast.LENGTH_SHORT).show();
}
}
else {
/* No estas conectado a internet */
Toast.makeText(getBaseContext(), "Verifique su conexión a internet ", Toast.LENGTH_SHORT).show();
}
Esta es mi clase adaptadornoticia.java
public class adaptadorNoticia extends RecyclerView.Adapter<adaptadorNoticia.ViewHolder> {
//private List<noticia> items;
/**
* Interfaz de comunicación
*/
public interface OnItemClickListener {
void onItemClick(ViewHolder item, int position);
}
private OnItemClickListener listener;
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
public OnItemClickListener getOnItemClickListener() {
return listener;
}
public static class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
// Campos respectivos de un item
public TextView titulo;
public TextView fecha;
public ImageView imagen;
private adaptadorNoticia padre = null;
public ViewHolder(View v, adaptadorNoticia padre) {
super(v);
v.setOnClickListener(this);
this.padre = padre;
titulo = (TextView) v.findViewById(R.id.titulo_noticia);
fecha = (TextView) v.findViewById(R.id.precio_fecha);
imagen = (ImageView) v.findViewById(R.id.miniatura_noticia);
}
@Override
public void onClick(View v) {
final OnItemClickListener listener = padre.getOnItemClickListener();
if (listener != null) {
listener.onItemClick(this, getAdapterPosition());
}
}
}
public adaptadorNoticia(){}
@Override
public long getItemId(int position) {
//return noticia.Ultimas_Noticias.get(position).getId();
return Long.parseLong(noticia.Ultimas_Noticias.get(position).getId());
}
public int getItemCount() {
return noticia.Ultimas_Noticias.size();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_noticia, viewGroup, false);
return new ViewHolder(v, this);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
noticia item = noticia.Ultimas_Noticias.get(i);
viewHolder.titulo.setText(item.getTitulo());
viewHolder.fecha.setText(item.getFecha());
Glide.with(viewHolder.itemView.getContext())
.load(item.getUrlImagen())
.centerCrop()
.into(viewHolder.imagen);
}
}
Saludos.
'int java.util.List.size()' on a null object reference