Aller au contenu

Problème lors d'un changement d'activité


dodo021

Recommended Posts

Bonjour tout le monde ! =)

Voila j'ai commencé la programmation sous Android depuis quelque temps et là j'ai un petit problème avec mon application.

Je vous explique. Mon application va rechercher un fichier XML depuis l'API de Steam et j'extrais les données qui m'intéresse en fonction du pseudo entré dans la première activité. Jusque là tout va bien. =)

448157vue1.png

J'ai mis la recherche des informations dans un thread afin que l'application n'affiche pas le message d'erreur comme quoi elle ne répond plus. De nouveau jusque là c'est tout bon =)

La seconde vue affiche l'image perso de la personne et son pseudo Steam et un menu qui donne le choix a l'utilisateur d'aller voir ces amis, ces jeux et son profile Steam.

946145vue2.png

Le problème viens quand je veux afficher l'activité pour voir ces amis qui doit afficher une simple liste. Mais le problème doit venir du thread. Voila le logcat lors de l'erreur.

09-26 13:26:23.348: WARN/dalvikvm(3548): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
09-26 13:26:23.358: ERROR/AndroidRuntime(3548): Uncaught handler: thread main exiting due to uncaught exception
09-26 13:26:23.368: ERROR/AndroidRuntime(3548): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pixeldroid.steamdroid/com.pixeldroid.steamdroid.Friend}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at android.os.Looper.loop(Looper.java:123)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at android.app.ActivityThread.main(ActivityThread.java:4203)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at java.lang.reflect.Method.invoke(Method.java:521)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at dalvik.system.NativeStart.main(Native Method)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at android.app.ListActivity.onContentChanged(ListActivity.java:236)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at android.app.Activity.setContentView(Activity.java:1620)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at com.pixeldroid.steamdroid.Friend.onCreate(Friend.java:14)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
09-26 13:26:23.368: ERROR/AndroidRuntime(3548):     ... 11 more

Voila le code source de l'activité.

package com.pixeldroid.steamdroid;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.style.BackgroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Home extends Activity {

   // Variable d'interface
   private TextView txtPseudo;
   private ImageView imgAvatar;
   private Button btnProfil;
   private Button btnGame;
   private Button btnFriend;

   private HttpClient client;
   private String url;
   private String pseudo;
   private ArrayList steamInfo = new ArrayList();

   String queryReturn = "";
   Boolean isRunning = false;
   Handler handlerGetInfo = new Handler(){
       public void handleMessage(Message msg){
           // Puis on affiche le resultat
           try {
               Log.i("Thread", "Debut du parsing des info");
               // Si des information on pu etre récupèrée on active les boutons
               // Sinon rien
               if(afficherInfo(queryReturn))
               {
                   btnFriend.setEnabled(true);
                   Toast.makeText(Home.this, "Bienvenue !", 2000).show();
               }
           } catch (Exception e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
       };
   };

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       // TODO Auto-generated method stub
       super.onCreate(savedInstanceState);
       setContentView(R.layout.home);

       // Récuperation du pseudo depuis la vue Main
       pseudo = this.getIntent().getStringExtra("pseudo");

       // On met en memoire tous les widgets
       txtPseudo = (TextView)findViewById(R.id.txtPseudo);
       imgAvatar = (ImageView)findViewById(R.id.imgAvatar);

       btnProfil = (Button)findViewById(R.id.btnProfil);
       btnGame = (Button)findViewById(R.id.btnGame);
       btnFriend = (Button)findViewById(R.id.btnFriend);

       // On désactive les boutons
       btnProfil.setEnabled(false);
       btnGame.setEnabled(false);
       btnFriend.setEnabled(false);
       // On affiche un message pour informer que les informations sont en coure de chargement
       Toast.makeText(this, "Chargement de vos informations", 3000).show();

   }

   @Override
   protected void onStart() {
       super.onStart();
       new Thread(new Runnable() {

           public void run(){
               // TODO Auto-generated method stub
               Log.i("Thread", "Demarrage du thread");
               try{
                   // TODO Auto-generated method stub
                   client = new DefaultHttpClient();

                   url = "http://steamcommunity.com/id/"+pseudo+"/?xml=1";

                   // Initialisation de la requête GET
                   HttpGet getQuery = new HttpGet(url);
                   ResponseHandler responseHandler = new BasicResponseHandler();

                   try {
                       Log.i("Thread", "Debut du telechargement des info");
                       // On télécharge le XML depuis l'url donnée
                       queryReturn = client.execute(getQuery, responseHandler);

                       Log.i("Thread", "Telechargement des info termine");
                       Log.i("Thread", "Envoie du message handle");
                       handlerGetInfo.sendMessage(handlerGetInfo.obtainMessage());

                   } catch (ClientProtocolException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                   } catch (IOException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                   }
                   Log.i("Thread", "Arret du thread");
               }
               catch(Throwable t)
               {
                   Log.i("Thread", "Arret du thread");
               }
           }
       }).start();
   }

   @Override
   protected void onPause() {
       // TODO Auto-generated method stub
       super.onPause();
   }

   @Override
   protected void onStop() {
       // TODO Auto-generated method stub
       super.onStop();
   }

   boolean afficherInfo(String xml) throws Exception{

       // On initialise le parser XML
       DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
       Document doc = builder.parse(new InputSource(new StringReader(xml)));

       // On crée une liste de noeud a partire du noeud source 
       NodeList forecastList = doc.getElementsByTagName("profile");

       // On vérifie que l'utilisateur existe
       if(forecastList.item(0) != null)
       {
           // Si le pseudo existe on parcoure le XML pour la recherche des infos
           for (int i = 0; i < forecastList.getLength(); i++) {
               Element currentFore = (Element)forecastList.item(i);
               steamInfo.add(getValue(currentFore.getElementsByTagName("steamID").item(0)));
               steamInfo.add(getValue(currentFore.getElementsByTagName("avatarFull").item(0)));
           }

           // On récupère l'avatar de l'utilisateur
           Bitmap avatar = getAvatar(steamInfo.get(1));

           // On affiche l'image et le pseudo de la personne
           imgAvatar.setImageBitmap(avatar);
           txtPseudo.setText(steamInfo.get(0));

           return true;
       }
       else
       {
           Toast.makeText(this, getString(R.string.error_comptnofound), 3000).show();
           return false;
       }
   }

   public String getValue(Node node){
       return node.getChildNodes().item(0).getNodeValue();
   }

   public Bitmap getAvatar(String url){
       Bitmap avatar = null;

       try{
           URL imgUrl = new URL(url);
           URLConnection connexion = imgUrl.openConnection();
           connexion.connect();

           InputStream stream = connexion.getInputStream();
           BufferedInputStream bufferStream = new BufferedInputStream(stream);
           avatar = BitmapFactory.decodeStream(bufferStream);
           bufferStream.close();
           stream.close();
       }
       catch(IOException e){

       }

       return avatar;
   }

   public void getFriendView(View btnFriend){
       Intent intentFriend = new Intent(Home.this, Friend.class);
       intentFriend.putExtra("pseudo", pseudo);

       startActivity(intentFriend);
   }
}

Voila le code de l'activité de la liste d'amis:

package com.pixeldroid.steamdroid;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Friend extends ListActivity {

   String[] friends = {"Amis1", "Amis2", "Amis3", "Amis4", "Amis5", "Amis6", "Amis7", "Amis8", "Amis9", "Amis10"};

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       // TODO Auto-generated method stub
       super.onCreate(savedInstanceState);
       setContentView(R.layout.friendslist);

       setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, friends));
   }

   @Override
   protected void onListItemClick(ListView l, View v, int position, long id) {
       // TODO Auto-generated method stub
       super.onListItemClick(l, v, position, id);
       Toast.makeText(this, friends[position],    1000).show();
   }
}

Voila j'espère que vous pourrez m'aider étant quand même débutant j'espère avoir de bon conseille pour la suite.

Merci d'avance ! =)

Lien vers le commentaire
Partager sur d’autres sites

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×
×
  • Créer...