2016-09-29 57 views
-1

MapActivity與搜索谷歌地圖掙扎的地方

public class Agriculture extends AppCompatActivity implements  
     OnMapReadyCallback, 
     GoogleMap.OnInfoWindowClickListener, 
     GoogleMap.OnMarkerClickListener { 

    GoogleMap mMap; 
    Address address; 
    LatLng latLng; 
    SupportMapFragment sMapFragment; 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_map); 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 


public void onSearch(View view) 


{ 

     EditText tfLocation = (EditText) findViewById(R.id.tfLocation); 

     String location = tfLocation.getText().toString(); 

     List<Address> addressList = null; 

     if (location!=null && !location.equals("")) 
     { 
      Geocoder geocoder = new Geocoder(this); 
      try { 

       addressList = geocoder.getFromLocationName(location, 0); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      Address address = addressList.get(0); 
      LatLng latLng = new LatLng(address.getLatitude() , address.getLongitude()); 

logcat的

Process: amahlo.gridview, PID: 13000 
java.lang.IllegalStateException: Could not execute method for android:onClick 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 
at android.view.View.performClick(View.java:5210) 
at android.view.View$PerformClick.run(View.java:20976) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:6145) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
Caused by: java.lang.reflect.InvocationTargetException 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5210)  
at android.view.View$PerformClick.run(View.java:20976)  
at android.os.Handler.handleCallback(Handler.java:739)  
at android.os.Handler.dispatchMessage(Handler.java:95)  
at android.os.Looper.loop(Looper.java:145)  
at android.app.ActivityThread.main(ActivityThread.java:6145)  
at java.lang.reflect.Method.invoke(Native Method)  
at java.lang.reflect.Method.invoke(Method.java:372)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)  
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference 
at amahlo.gridview.Agriculture.onSearch(Agriculture.java:45) 
at java.lang.reflect.Method.invoke(Native Method)  
at java.lang.reflect.Method.invoke(Method.java:372)  
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)  
at android.view.View.performClick(View.java:5210)  
at android.view.View$PerformClick.run(View.java:20976)  
at android.os.Handler.handleCallback(Handler.java:739)  
at android.os.Handler.dispatchMessage(Handler.java:95)  
at android.os.Looper.loop(Looper.java:145)  
at android.app.ActivityThread.main(ActivityThread.java:6145)  
at java.lang.reflect.Method.invoke(Native Method)  
at java.lang.reflect.Method.invoke(Method.java:372)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)  
+1

錯誤發生在'Agriculture.java'的第45行。可能是你的'addressList'爲空。如果沒有找到匹配或沒有後端服務可用,getFromLocationName()將返回空或空列表。 –

+0

提供完整的佈局和活動代碼plz –

+0

提供完整的代碼b'z您在onclick中有錯誤,因此可能會丟失一些東西。所以我們可以指出。 – androidnoobdev

回答

0

忽略的事實是location不能null ...

你先設置列表爲空。

List<Address> addressList = null; 

然後繼續檢查字符串是否爲空並做一些嘗試。

if (!TextUtils.isEmpty(location)) 
{ 
    Geocoder geocoder = new Geocoder(this); 
    try { 
     addressList = geocoder.getFromLocationName(location, 0); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

然而,在這裏,如果有一個例外,addressList仍然是空,因此您的錯誤。

Address address = addressList.get(0); 

沒有看到實際的異常,很難給出一個完整的答案,但快速的解決方案,以防止空指針是最後一行移動到try(和可選檢查!addressList.isEmpty()試圖訪問之前其數據)。