1

是否可以在 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                  
            }
        }

我得到了这样的错误 在此处输入图像描述

4

3 回答 3

1

this指的是new Runnable(),里面没有方法getResource()

改为使用Car_finder_oneActivity.this.getResource().getDrawable(R.drawable.park_here_icon);

于 2012-10-27T10:04:03.210 回答
0

在这里,这将引用您正在使用的匿名 Runnable 类。runnable 没有该方法。而是使用

drawable = getApplication().getResources().getDrawable(R.drawable.park_here_icon);
于 2012-10-27T10:05:39.033 回答
0

你需要有一个Context像这样的对象

Context context=getApplicationContext();
drawable=context.getResources().getDrawable(R.drawable.park_here_icon);
于 2012-10-27T10:07:03.490 回答