IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Problème de listAdapter


Sujet :

Android

  1. #1
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Août 2008
    Messages
    1
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Problème de listAdapter
    bonjour à tous et toutes,

    je débute un peu dans la prog pour application android et je suis confronté à un peu soucis lors d'un remplissage de listView.

    la compilation se passe bien, l'appli se lance, mais lorsque le bouton de récupération des données sur l'url se fait, le Simple Adapter plante.

    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at xxxx.xx.MainActivity$JSONParse.onPostExecute(MainActivity.java:185)


    Impossible de savoir si c'est le context , la list de item listItem est bien remplie, du coup je ne vois pas.

    Merci par avance pour votre aide.

    voici mon code ci-dessous



    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
     
     
    import android.os.AsyncTask;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.app.TabActivity;
    import android.content.Intent;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.view.View;
     
     
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TabHost;
    import android.app.ProgressDialog;
    import android.util.Log;
    import android.widget.Button;
     
     
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
     
    import java.util.ArrayList;
    import java.util.HashMap;
     
    import com.equitanet.www.actuequitanet.JsonParser;
     
    public class MainActivity extends TabActivity {
        Button Btngetdata;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            Resources res = getResources();
            TabHost tabHost = getTabHost();
            TabHost.TabSpec spec;
            Intent intent;
     
            intent = new Intent().setClass(this, Tab1.class);
     
            spec = tabHost
                    .newTabSpec("Widget")
                    .setIndicator("Actualités",
                            res.getDrawable(android.R.drawable.ic_menu_today))
                    .setContent(intent);
            tabHost.addTab(spec);
     
            intent = new Intent().setClass(this, Tab2.class);
            spec = tabHost
                    .newTabSpec("onglet2")
                    .setIndicator("Numéros utiles",
                            res.getDrawable(android.R.drawable.ic_menu_manage))
                    .setContent(intent);
            tabHost.addTab(spec);
     
            intent = new Intent().setClass(this, Tab3.class);
            spec = tabHost
                    .newTabSpec("onglet3")
                    .setIndicator("Infos société", res.getDrawable(android.R.drawable.ic_menu_mapmode))
                    .setContent(intent);
            tabHost.addTab(spec);
     
            tabHost.setCurrentTab(0);
     
            Btngetdata = (Button)findViewById(R.id.getdata);
            Btngetdata.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new JSONParse().execute();
                }
            });
     
     
            // appel les news //
            //new JSONParse().execute();
     
     
     
     
        }
     
     
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
     
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
     
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
     
            return super.onOptionsItemSelected(item);
        }
     
     
     
     
        private class JSONParse extends AsyncTask<String, String, JSONObject>
        {
            private ListView listeviewnews;
            private  String url = "http://www.xxxx.xx/xxxxx.php";
            private  String TAG_TITRE = "titrenews";
            private  String TAG_AUTEUR = "auteurnews";
            private  String TAG_CONTENU = "contenu";
            private  String TAG_DATENEWS = "datenews";
            private  String TAG_IDNEWS = "id";
            private ProgressDialog pDialog;
     
     
            @Override
            protected void onPreExecute() {
                /*super.onPreExecute();
                pDialog = new ProgressDialog(MainActivity.this);
                pDialog.setMessage("Chargement des news. Merci de patienter...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();*/
            }
     
            @Override
            protected JSONObject doInBackground(String... args)
            {
                JsonParser jParser = new JsonParser();
                JSONObject json = jParser.getJSONFromUrl(url);
     
                return json;
     
            }
     
            @Override
            protected void onPostExecute (JSONObject json)
            {
                ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
                HashMap<String, String> map;
     
                Log.e("log_tag", "Lancement de la tache post execute" );
     
     
                // recuperation de la listeView cree dans le main_activity.xml //
                listeviewnews = (ListView) findViewById(R.id.listeviewnews);
     
                try
                {
                    JSONArray newslist = json.getJSONArray("listenews");
                    for( int i=0; i < newslist.length();i++)
                    {
                        JSONObject e = newslist.getJSONObject(i);
     
                        map = new HashMap<String, String>();
                        map.put("titrenews",e.getString("titre"));
                        map.put("descriptionnews",e.getString("contenu"));
                        map.put("datenews",e.getString("datenews"));
                        map.put("imgnews",String.valueOf(R.drawable.img_assurance_cheval_64));
     
                        listItem.add(map);
     
     
                        Log.e("log_tag", "titre :" + e.getString("titre"));
     
                    }
     
     
                    // creation de l'adapter qui renseignera les items de la liste  affichagenumero //
                    SimpleAdapter mSchedule = new SimpleAdapter (getApplicationContext(), listItem, R.layout.affichagenews,
                            new String[] {"imgnews", "titrenews", "descriptionnews","datenews"}, new int[] {R.id.imgnews, R.id.titrenews, R.id.descriptionnews, R.id.datenews}
                    );
     
                    // attribue à la listview l'adapter //
                    listeviewnews.setAdapter(mSchedule);
     
     
                   // pDialog.dismiss();
     
     
     
     
     
     
     
                }
                catch(JSONException e)
                {
                    e.printStackTrace();
     
                }
     
            }
     
     
     
     
     
     
     
     
        }
     
     
     
     
     
     
     
     
     
     
     
    }

  2. #2
    Membre actif Avatar de Altak
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2014
    Messages
    170
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2014
    Messages : 170
    Points : 245
    Points
    245
    Par défaut
    Bonjour,

    D’après ton erreur, il semle que le retour de ton findViewById() soit null.
    Je ne sais pas ce que retourne cette fonction lorsqu'elle est appelé depuis une asyncTask, ca peut effectivement venir du context ou du fait que tu n'ai pas de widget avec l'id "listeviewnews"
    As-tu essayé de créer une fonction dans ton activity qui sera appelé par ton doInBackground() ?

    exemple:
    dans ton activity, tu creer une fonction callback(data);
    dans ton AsyncTask, tu t'occupe de traiter le retour de ta requete mais tu ne met as a jour ta liste et ensuite tu appelle la fonction que tu a créé dans ton activity qui s’occupera de la mettre a jour.

    gl hf

Discussions similaires

  1. Problème d'installation oracle 8.1.7 sous NT
    Par Anonymous dans le forum Installation
    Réponses: 7
    Dernier message: 02/08/2002, 14h18
  2. Problème d'impression
    Par IngBen dans le forum C++Builder
    Réponses: 7
    Dernier message: 22/05/2002, 11h37
  3. Problème avec la mémoire virtuelle
    Par Anonymous dans le forum CORBA
    Réponses: 13
    Dernier message: 16/04/2002, 16h10
  4. Réponses: 6
    Dernier message: 25/03/2002, 21h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo