Quando eu insiro o if nos campos endereco e site, aparece esta fatal exception:
package br.com.rodrigo.agendinha.adapter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import br.com.rodrigo.agendinha.ListaAlunosActivity;
import br.com.rodrigo.agendinha.R;
import br.com.rodrigo.agendinha.modelo.Aluno;
/**
* Created by Rodrigo on 8/12/2016.
*/
public class AlunosAdapter extends BaseAdapter {
private final List<Aluno> alunos;
private final Context context;
public AlunosAdapter(Context context, List<Aluno> alunos) {
this.context = context;
this.alunos = alunos;
}
@Override
public int getCount() {
return alunos.size();
}
@Override
public Object getItem(int i) {
return alunos.get(i);
}
@Override
public long getItemId(int i) {
return alunos.get(i).getId();
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Aluno aluno = alunos.get(i);
LayoutInflater inflater = LayoutInflater.from(context);
View view1 = view;
if (view == null) {
view1 = inflater.inflate(R.layout.list_item, viewGroup, false);
}
TextView campoNome = (TextView) view1.findViewById(R.id.item_nome);
campoNome.setText(aluno.getNome());
TextView campoTelefone = (TextView) view1.findViewById(R.id.item_telefone);
campoTelefone.setText(aluno.getTelefone());
TextView campoEndereco = (TextView) view.findViewById(R.id.item_endereco);
if (campoEndereco != null) {
campoEndereco.setText(aluno.getEndereco());
}
TextView campoSite = (TextView) view.findViewById(R.id.item_site);
if (campoSite != null) {
campoSite.setText(aluno.getSite());
}
ImageView campoFoto = (ImageView) view1.findViewById(R.id.item_foto);
String caminhoFoto = aluno.getCaminhoFoto();
if (caminhoFoto != null) {
Bitmap bitmap = BitmapFactory.decodeFile(caminhoFoto);
Bitmap bitmapReduzido = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
campoFoto.setImageBitmap(bitmapReduzido);
campoFoto.setScaleType(ImageView.ScaleType.FIT_XY);
}
return view1;
}
}
//Esta é a mensagem de erro:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.rodrigo.agendinha, PID: 8802
java.lang.NullPointerException
at br.com.rodrigo.agendinha.adapter.AlunosAdapter.getView(AlunosAdapter.java:63)
at android.widget.AbsListView.obtainView(AbsListView.java:2306)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16574)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
at android.view.View.measure(View.java:16574)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:16574)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:16574)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16574)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1406)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:697)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
at android.view.View.measure(View.java:16574)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2425)
at android.view.View.measure(View.java:16574)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1960)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1156)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1338)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1043)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5916)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)