2012-10-27 33 views
1

是否可以使用Runnable()內部的getResources()方法? 。當我使用這種方法時,它顯示一個錯誤。我想獲取在線數據庫數據並將其用於地圖。但是,迴應爲空。任何一個可以幫助我解決這個錯誤,或者發現了它的選擇,這裏是我的課getResources()在Runnable中顯示錯誤

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     
      } 
     } 

我得到了錯誤這樣 enter image description here

+0

試試這個(Drawable)getResources()。getDrawable(R.drawable.park_here_icon) –

+0

'Context context; drawable = context.getResources()。getDrawable(R.drawable.park_here_icon)'也試試這個 – Zombie

+0

試着給這樣的 - 'Car_finder_oneActivity.this.getResources()。(R.drawable.park_here_icon);' – Praveenkumar

回答

0

,你需要有一個Context對象這樣

Context context=getApplicationContext(); 
drawable=context.getResources().getDrawable(R.drawable.park_here_icon); 
1

thisnew Runnable(),其中沒有一個方法getResource()

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

0

這裏,這將是指你在工作匿名的Runnable類的。可運行的不具有method.Instead使用

drawable = getApplication().getResources().getDrawable(R.drawable.park_here_icon); 
+0

沒有包標識符當獲得資源編號xxxxxxxx的值時 – sonichy