toshiro92
toshiro92

Reputation: 1344

NullPointerException problem on a ListView when set setOnItemClickListener

I have a problem with a nullPointer :

 FATAL EXCEPTION: main
 java.lang.NullPointerException
 at com.irdes.adapter.PersonneAdapter.getView(PersonneAdapter.java:66)
 at android.widget.AbsListView.obtainView(AbsListView.java:1430)
 at android.widget.ListView.makeAndAddView(ListView.java:1745)
 at android.widget.ListView.fillDown(ListView.java:670)
 at android.widget.ListView.fillFromTop(ListView.java:727)
 at android.widget.ListView.layoutChildren(ListView.java:1598)
 at android.widget.AbsListView.onLayout(AbsListView.java:1260)
 at android.view.View.layout(View.java:7175)
 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
 at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
 at android.view.View.layout(View.java:7175)
 at android.widget.SlidingDrawer.onLayout(SlidingDrawer.java:331)
 at android.view.View.layout(View.java:7175)
 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
 at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1243)
 at android.widget.LinearLayout.onLayout(LinearLayout.java:1049)
 at android.view.View.layout(View.java:7175)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
 at android.view.View.layout(View.java:7175)
 at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
 at android.view.View.layout(View.java:7175)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
 at android.view.View.layout(View.java:7175)
 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
 at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
 at android.view.View.layout(View.java:7175)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
 at android.view.View.layout(View.java:7175)
 at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
 at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:123)
 at android.app.ActivityThread.main(ActivityThread.java:3683)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:507)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 at dalvik.system.NativeStart.main(Native Method)

The error pointed by logcat is on my class personneAdapter (error line is after **):

[...]
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if(convertView == null) {

        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.affichagelistview, null);
        //NB : j'ai réutilisé affichagelistview des actualités pour gagner du temps, et de la place
        holder.nomPers = (TextView)convertView.findViewById(R.id.titreActu);
        holder.descrPers = (TextView)convertView.findViewById(R.id.dateActu);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
        ** holder.nomPers.setText((pers.get(position)).getNom());
        holder.descrPers.setText((pers.get(position)).getDescr());

    return convertView;

}

But i founded where the real problem error is, on this code (error line zone is after **):

public void initListDetailPersonne(ListView liste, int typeDetail, int num) {
[...]
PersonneAdapter adapter = new PersonneAdapter(context, listePersonnes);
    liste.setAdapter(adapter); 
    liste.setTextFilterEnabled(true);
    **liste.setOnItemClickListener(new OnItemClickListener() {
        **public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            Personne pers = (Personne) a.getItemAtPosition(position);
            Intent intent = new Intent(context,DetailActivity.class);
            //NB : Le numéro correspond à typeUpdate, le code à l'Id de l'objet à récupérer
            //L'envoi de l'objet entre deux activity est compliqué et couteux à mettre en place
            //L'activity qui réceptionnera les données ira elle même dans la BDD interne
            Bundle objetbunble = new Bundle();
            objetbunble.putString("Classe", "2");
            objetbunble.putString("Code", pers.getNum()+"");
            // on passe notre objet a notre activities
            intent.putExtras(objetbunble );
           // on appelle notre activité
            context.startActivity(intent);


        }
    });

The origin of this call is this class :

[...]
break;
        case 9:
            Seminaire sem = db.getUnSeminaire(codeObject);
            titre.setText(sem.getNom());
            telOrDate.setText(sem.getDate()+" "+sem.getHeure());
            descr.setText(sem.getDescr());
            // ------------------------------------- a modifier --------------------------- //
            buttonLien.setText(sem.getLien());
            ILVOA.initListDetailPersonne(listView, 9, sem.getNum());
            titreSliding.setText("Orateurs");
        break;
    }

I used the same code for others views and adapters, but only this code doesn't work with seminaire.

Upvotes: 0

Views: 768

Answers (1)

blender
blender

Reputation: 361

When does the error fire? Upon clicking an listitem or already when showing the ListView? As for the LogCat the npe come either from holder.nomPers.setText, pers.get() or pers.getNom(). If it is just a Number you want to store, you may write it to the Tag of your TextViews. Syntax would be then:

myPersNum = ((View)v.findViewById(R.id.titreActu)).getTag();

Upvotes: 1

Related Questions