Is it possible to use the getResources() method inside the Runnable() ? . When i use this method it shows an error. I am want to get online db data and use it for map. But, the response is null. Can any one help me to fix this error or find its alternatives and here is my class
public class Car_finder_oneActivity extends Activity implements LocationListener{
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
LocationManager locManager;
GeoPoint point;
Drawable drawable;
CarItemizedOverlay itemizedOverlay;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cancel_park);
dialog = ProgressDialog.show(Car_finder_oneActivity.this, "", "Cancel parking...", true);
new Thread(new Runnable() {
public void run() {
cancelpark();
}
}).start();
}
public void cancelpark(){
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://10.0.2.2/test_login/latest.php");
nameValuePairs = new ArrayList<NameValuePair>(2);
String p_u_name = "admin";
String KEY_MAP = "map";
nameValuePairs.add(new BasicNameValuePair(KEY_MAP,""));
nameValuePairs.add(new BasicNameValuePair("username",p_u_name.trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
System.out.println("Response from PHP : " + response);
dialog.dismiss();
}
});
if(response!=null)//.equalsIgnoreCase("Success"))
{
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(Car_finder_oneActivity.this,response, Toast.LENGTH_SHORT).show();
String data = response;
String items[] = data.split(",");
String lat = items[0];
int latitude = Integer.parseInt(lat);
String lon = items[1];
int longitude = Integer.parseInt(lon);
point = new GeoPoint((int)(latitude*1E6),(int)(longitude *1E6));
MapView mapView = (MapView) findViewById(R.id.carfinder_mapview);
//get the MapController object
MapController controller = mapView.getController();
controller.animateTo(point);
// fetch the drawable - the pin that will be displayed on the map
drawable = this.getResources().getDrawable(R.drawable.park_here_icon);
// create and add an OverlayItem to the MyItemizedOverlay list
OverlayItem overlayItem = new OverlayItem(point, "", "");
itemizedOverlay = new CarItemizedOverlay(drawable,this);
itemizedOverlay.addOverlay(overlayItem);
// add the overlays to the map
mapView.getOverlays().add(itemizedOverlay);
mapView.invalidate();
}
});
//startActivity(new Intent(LoginActivity.this, MainActivity.class));
}
else{
Toast.makeText(Car_finder_oneActivity.this,"foo", Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
public void showAlert(){
Car_finder_oneActivity.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Car_finder_oneActivity.this);
builder.setTitle("Parking Error.");
builder.setMessage("Try again?.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent r = new Intent(getApplicationContext(),Park_HereActivity.class);
startActivity(r);
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
I got the error like this
you need to have a Context
object like this
Context context=getApplicationContext();
drawable=context.getResources().getDrawable(R.drawable.park_here_icon);