是否可以在Runnable()中使用getResources()方法? 。当我使用此方法时,它显示错误。我想获取在线数据库数据并将其用于地图。但是,响应为空。任何人都可以帮我解决这个错误或找到它的替代方案,这是我的班级
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
}
}
我得到了这样的错误
答案 0 :(得分:1)
this
是指new Runnable()
,其中没有方法getResource()
。
改为使用Car_finder_oneActivity.this.getResource().getDrawable(R.drawable.park_here_icon);
。
答案 1 :(得分:0)
这里将引用你正在使用的匿名Runnable类。一个runnable没有那个方法。相当使用
drawable = getApplication().getResources().getDrawable(R.drawable.park_here_icon);
答案 2 :(得分:0)
你需要有一个Context
这样的对象
Context context=getApplicationContext();
drawable=context.getResources().getDrawable(R.drawable.park_here_icon);