0

這是我調用GPS座標的方法。它使用GUI上的按鈕onclick工作得很好。但是,每當我嘗試從其他方法調用它,它會使我的應用程序崩潰。無法從其他方法調用此方法

public void getPattern(View v){ 
    String s = "http://maps.google.com/maps?f=q&hl=en&q=05.445560,100.424200&ie=UTF8&z=16&iwIoc=addr&om=1speed:000.0&imei=355689019472548"; 
    // String s = messageBox.getText().toString(); 
    // Log.d("check", s); 
    Pattern p = Pattern.compile("\\d+\\.\\d+"); 
    Matcher m = p.matcher(s); 
    int i =0; 
    Float n[] = new Float[4]; 
     while (m.find()) { 
       System.out.println(m.group()); 
       n[i]= Float.parseFloat(m.group()); 
       i++; 
      } 

      LatLng locLatLng = new LatLng(n[0],n[1]); 
      CameraPosition cameraPosition = new CameraPosition.Builder().target(locLatLng).zoom(15).build(); 
      googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
      MarkerOptions marker = new MarkerOptions().position(locLatLng).title("Here's your car dude!!!").snippet("Found it!!"); 
      marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); 
      googleMap.addMarker(marker).showInfoWindow(); 

} 

迄今爲止從LatLng開始的錯誤。如果我刪除了這段代碼,當然還有(視圖v)和主要方法的調用,如getPattern();它工作的很好,但我需要LatLng代碼的其餘部分,以便它可以自動從提取的Lat長度的字符串s中獲取位置。 n變量正確捕獲經緯度數據。

我的意思是我想從代碼中調用這個方法,如在onCreate或其他地方沒有與按鈕或任何GUI元素交互。例如我有這種方法

public static void updateMessageBox(String msg) { 
    messageBox.setText(msg); 
    getPattern(); 
} 

這導致我的應用程序崩潰。如果我開始卸下LatLng代碼它工作得很好,也拆除(View v)

+0

顯示在您試圖調用這個函數 –

+0

updatemessagebox方法調用getPattern method..I功能發現錯誤,但未能解決it..the罪魁禍首是一個String,我使用。我無法使用GUI的任何變量輸入。我只能在方法中使用靜態變量define。 – slumberj

回答

0

,如果你要撥打的按鈕的onClick功能把內容在新method,然後調用的onClick函數內部的功能。

public void getPattern(View v){ 

    getPatternmatch(); 
    } 



    public void getPatternmatch(){ 
     String s = "http://maps.google.com/maps?f=q&hl=en&q=05.445560,100.424200&ie=UTF8&z=16&iwIoc=addr&om=1speed:000.0&imei=355689019472548"; 
     // String s = messageBox.getText().toString(); 
     // Log.d("check", s); 
     Pattern p = Pattern.compile("\\d+\\.\\d+"); 
     Matcher m = p.matcher(s); 
     int i =0; 
     Float n[] = new Float[4]; 
      while (m.find()) { 
        System.out.println(m.group()); 
        n[i]= Float.parseFloat(m.group()); 
        i++; 
       } 

       LatLng locLatLng = new LatLng(n[0],n[1]); 
       CameraPosition cameraPosition = new CameraPosition.Builder().target(locLatLng).zoom(15).build(); 
       googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
       MarkerOptions marker = new MarkerOptions().position(locLatLng).title("Here's your car dude!!!").snippet("Found it!!"); 
       marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); 
       googleMap.addMarker(marker).showInfoWindow(); 

    } 
+0

您無法直接調用onClick方法的按鈕...將onClick方法中的內容放到另一個方法中,並調用該函數而不是onClick方法 – Nambi