Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Fatal Exception quando eu insiro o if nos campos endereco e site

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)
2 respostas

Segue a minha layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@mipmap/ic_pessoa"
        android:id="@+id/item_foto"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/item_nome"
            android:text="Nome"
            android:textSize="28sp"
            android:textStyle="bold"
            android:paddingLeft="10dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/item_telefone"
            android:text="Telefone"
            android:textSize="24sp"
            android:paddingLeft="10dp"
            android:paddingTop="8dp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="right"
        android:layout_marginRight="16dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/item_endereco"
            android:text="Endereco"
            android:textSize="24sp"
            android:paddingTop="8dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/item_site"
            android:text="Site"
            android:textSize="24sp"
            android:paddingTop="10dp"/>

    </LinearLayout>



</LinearLayout>
solução!

Já resolvi. Eu deveria ter colocado view1 em vez de view...