L'erreur FATAL EXCEPTION: AsyncTask #1

youmari - 16 févr. 2013 à 23:28
 youmari - 20 févr. 2013 à 12:17
Bonsoir ,
je suis nouvelle dans le developement des applications android et j'avais besoin de faire de la géolocalisation à partir du google map avec une liste des ressources auprès de ma position et j'ai arrivé à trouver un code sur le net mais ce code ne marche pas quand j'ai essayé de l'integrer dans mon projet c'est à dire que je voulais en cliquant sur un button ouvrir cette activité mais ça ne marche pas et pourtant ce projet marche bien tout seul et voilà les erreurs qui s'affichent :
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
.......


voilà la classe que j'utilise


package geolocalisation.com;

import java.util.ArrayList;
import java.util.HashMap;

import com.deaux.fansample.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class MainActivity extends Activity {

// flag for Internet connection status
Boolean isInternetPresent = false;

// Connection detector class
ConnectionDetector cd;

// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();

// Google Places
GooglePlaces googlePlaces;

// Places List
PlacesList nearPlaces;

// GPS Location
GPSTracker gps;

// Button
Button btnShowOnMap;

// Progress dialog
ProgressDialog pDialog;

// Places Listview
ListView lv;

// ListItems data
ArrayList<HashMap<String, String>> placesListItems = new ArrayList<HashMap<String,String>>();


// KEY Strings
public static String KEY_REFERENCE = "reference"; // id of the place
public static String KEY_NAME = "name"; // name of the place
public static String KEY_VICINITY = "vicinity"; // Place area name

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

cd = new ConnectionDetector(getApplicationContext());

// Check if Internet present
isInternetPresent = cd.isConnectingToInternet();
if (!isInternetPresent) {
// Internet Connection is not present
alert.showAlertDialog(MainActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}

// creating GPS Class object
gps = new GPSTracker(this);

// check if GPS location can get
if (gps.canGetLocation()) {
Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude());
} else {
// Can't get user's current location
alert.showAlertDialog(MainActivity.this, "GPS Status",
"Couldn't get location information. Please enable GPS",
false);
// stop executing code by return
return;
}

// Getting listview
lv = (ListView) findViewById(R.id.list);

// button show on map
btnShowOnMap = (Button) findViewById(R.id.btn_show_map);

// calling background Async task to load Google Places
// After getting places from Google all the data is shown in listview
new LoadPlaces().execute();

/** Button click event for shown on map */
btnShowOnMap.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(),
PlacesMapActivity.class);
// Sending user current geo location
i.putExtra("user_latitude", Double.toString(gps.getLatitude()));
i.putExtra("user_longitude", Double.toString(gps.getLongitude()));

// passing near places to map activity
i.putExtra("near_places", nearPlaces);
// staring activity
startActivity(i);
}
});


/**
* ListItem click event
* On selecting a listitem SinglePlaceActivity is launched
* */
lv.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String reference = ((TextView) view.findViewById(R.id.reference)).getText().toString();

// Starting new intent
Intent in = new Intent(getApplicationContext(),
SinglePlaceActivity.class);

// Sending place refrence id to single place activity
// place refrence id used to get "Place full details"
in.putExtra(KEY_REFERENCE, reference);
startActivity(in);
}
});
}

/**
* Background Async Task to Load Google places
* */
class LoadPlaces extends AsyncTask<String, String, String> {

/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
//super.onPreExecute();
/* pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage(Html.fromHtml(" Search
Loading Places..."));
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();*/

}

/**
* getting Places JSON
* */
protected String doInBackground(String... args) {
// creating Places class object
googlePlaces = new GooglePlaces();

try {
// Separeate your place types by PIPE symbol "|"
// If you want all types places make it as null
// Check list of types supported by google
//
String types = "cafe|restaurant"; // Listing places only cafes, restaurants

// Radius in meters - increase this value if you don't find any places
double radius = 1000; // 1000 meters

// get nearest places
nearPlaces = googlePlaces.search(gps.getLatitude(),
gps.getLongitude(), radius, types);


} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* After completing background task Dismiss the progress dialog
* and show the data in UI
* Always use runOnUiThread(new Runnable()) to update UI from background
* thread, otherwise you will get error
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
//pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed Places into LISTVIEW
* */
// Get json response status
String status = nearPlaces.status;

// Check for all possible status
if(status.equals("OK")){
// Successfully got places details
if (nearPlaces.results != null) {
// loop through each place
for (Place p : nearPlaces.results) {
HashMap<String, String> map = new HashMap<String, String>();

// Place reference won't display in listview - it will be hidden
// Place reference is used to get "place full details"
map.put(KEY_REFERENCE, p.reference);

// Place name
map.put(KEY_NAME, p.name);


// adding HashMap to ArrayList
placesListItems.add(map);
}
// list adapter
ListAdapter adapter = new SimpleAdapter(MainActivity.this, placesListItems,
R.layout.list_item,
new String[] { KEY_REFERENCE, KEY_NAME}, new int[] {
R.id.reference, R.id.name });

// Adding data into listview
lv.setAdapter(adapter);
}
}
else if(status.equals("ZERO_RESULTS")){
// Zero results found
alert.showAlertDialog(MainActivity.this, "Near Places",
"Sorry no places found. Try to change the types of places",
false);
}
else if(status.equals("UNKNOWN_ERROR"))
{
alert.showAlertDialog(MainActivity.this, "Places Error",
"Sorry unknown error occured.",
false);
}
else if(status.equals("OVER_QUERY_LIMIT"))
{
alert.showAlertDialog(MainActivity.this, "Places Error",
"Sorry query limit to google places is reached",
false);
}
else if(status.equals("REQUEST_DENIED"))
{
alert.showAlertDialog(MainActivity.this, "Places Error",
"Sorry error occured. Request is denied",
false);
}
else if(status.equals("INVALID_REQUEST"))
{
alert.showAlertDialog(MainActivity.this, "Places Error",
"Sorry error occured. Invalid Request",
false);
}
else
{
alert.showAlertDialog(MainActivity.this, "Places Error",
"Sorry error occured.",
false);
}
}
});

}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}



}

2 réponses

cormandyr Messages postés 600 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 8 juillet 2016 11
20 févr. 2013 à 11:05
Salut,

première petite remarque, utilises les balises de code la prochaine fois, cela le formatera et le rendra plus facilement lisible ;-)

Deuxieme chose, peux-tu nous donner la stack complète de ton exception...

Comme cela, je dois dire qu'il n'est pas facile de savoir quelle ligne pose soucis... je soupsonne la ligne
nearPlaces = googlePlaces.search(gps.getLatitude(), gps.getLongitude(), radius, types); 


mais je n'en suis pas sur....

Ce que tu peux deja faire c'est afficher en console les valeurs données à cette méthode... et comparer ces meme valeurs entre l'execution de ton application et celle que tu as téléchargée... (si tu vois ce que je veux dire)
0
Merci infiniment pour votre interet et vos conseils, mais finalement j'ai pu résoudre le problème c'était au niveau du dialog qui s'affiche
0
Rejoignez-nous