0

下面的活動從我的解析數據庫中獲取數據並顯示它。我有一個函數getLocationFromAddress()將字符串地址轉換爲LatLng格式。函數prepareMap()然後生成一個標記,然後指向地圖上的那個位置。 (未示出未imp的函數)該活動的代碼如下MapFragment崩潰不一致

public class SingleRestraunt extends ActionBarActivity { 
    private GoogleMap map; 
    TextView resteName, resteCuisine, resteLocation, resteAddress, restePrice, 
      resteTimings, restePayment, resteRating, resteWebsite, next, prev; 
    String restName, obj, restCuisine, restLocation, restAddress, restPrice, 
      restTimings, restPayment, restRating, restWebsite; 
    String[] menu; 
    JSONArray restmenu; 
    WebView web; 
    int i = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_single_restraunt); 
     resteName = (TextView) findViewById(R.id.restrauntName); 
     resteCuisine = (TextView) findViewById(R.id.restrauntCuisine); 
     resteLocation = (TextView) findViewById(R.id.restrauntLocation); 
     resteAddress = (TextView) findViewById(R.id.restrauntAddress); 
     restePrice = (TextView) findViewById(R.id.restrauntPrice); 
     resteTimings = (TextView) findViewById(R.id.restrauntTimings); 
     restePayment = (TextView) findViewById(R.id.restrauntPayment); 
     resteRating = (TextView) findViewById(R.id.restrauntRating); 
     resteWebsite = (TextView) findViewById(R.id.restrauntWebsite); 
     web = (WebView) findViewById(R.id.webView1); 
     next = (TextView) findViewById(R.id.next); 
     prev = (TextView) findViewById(R.id.prev); 

     Intent i = getIntent(); 
     obj = i.getStringExtra("restId"); //gets the id of the restaurant whose 
     getDetails(obj);     //details to be show 

    } 

    private void getDetails(String obj) { 

     ParseQuery<ParseObject> query = ParseQuery.getQuery("resdb"); 
     query.getInBackground(obj, new GetCallback<ParseObject>() { 

      @Override 
      public void done(ParseObject object, ParseException e) { 
       if (e == null) { 
        restName = object.getString("name"); 
        restCuisine = object.getString("cuisine"); 
        restLocation = object.getString("locality"); 
        restAddress = object.getString("address"); 
        restPrice = object.getString("price"); 
        restTimings = object.getString("timings"); 
        restPayment = object.getString("accepted"); 
        restRating = object.getString("ratings"); 
        restWebsite = object.getString("URL"); 
        restmenu = object.getJSONArray("menu"); 
        addData(); 
        // prepareMap(); 
       } else { 
        e.printStackTrace(); 
       } 
      } 
     }); 

    } 

    public void addData() { 
     resteName.setText(restName); 
     resteCuisine.setText(restCuisine); 
     resteLocation.setText(restLocation); 
     resteAddress.setText(restAddress); 
     restePrice.setText(restPrice); 
     resteTimings.setText(restTimings); 
     restePayment.setText(restPayment); 
     resteRating.setText(restRating); 
     resteWebsite.setText(restWebsite); 
     if (restmenu != null) { 
      try { 
       String menu = (String) restmenu.get(i); 
       getMenu(menu); 
      } catch (JSONException e1) { 
       e1.printStackTrace(); 
      } 

     } 
    } 

     public LatLng getLocationFromAddress(String strAddress) { 
     Geocoder coder = new Geocoder(this); 
     List<Address> address; 
     LatLng p1 = null; 
     try { 
     address = coder.getFromLocationName(strAddress, 5); 
     if (address != null && address.size() > 0) { 
      Address location = address.get(0); 
      p1 = new LatLng(location.getLatitude(), location.getLongitude()); 
     } else { 
      p1 = new LatLng(19.111258, 72.908313); 
     } 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return p1; 
     } 
     return p1; 
    } 

    public void prepareMap() { 
     final LatLng REST = getLocationFromAddress(restAddress + "," 
       + restLocation); 
     int zoomNiveau = 15; 
     map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     map.addMarker(new MarkerOptions().position(REST).title(restName));//New error points here 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(REST, zoomNiveau)); 
} 

最有趣的是上述活性不會崩潰每一次。它似乎只爲某些餐館而崩潰。錯誤日誌顯示如下

09-03 06:57:11.667: E/AndroidRuntime(2574): Process: com.example.gastronomaapp, PID: 2574 
09-03 06:57:11.667: E/AndroidRuntime(2574): java.lang.NullPointerException 
09-03 06:57:11.667: E/AndroidRuntime(2574):  at com.example.gastronomaapp.SingleRestraunt.prepareMap(SingleRestraunt.java:204) 

絕對不知道什麼是問題。我不明白什麼是邏輯問題,因爲它似乎適用於某些餐廳。任何想法?編輯在評論中提出了一個快速更改建議。但問題仍然存在,但只有錯誤已經改變

回答

2

你的問題很可能是在此代碼:

address = coder.getFromLocationName(strAddress, 5); 
     if (address != null) { 
      Address location = address.get(0); 
      p1 = new LatLng(location.getLatitude(), location.getLongitude()); 
     } else { 
      p1 = new LatLng(19.111258, 72.908313); 
     } 

確保地址列表是不是空的,你嘗試獲得第一個地址之前,添加類似這樣的:

address = coder.getFromLocationName(strAddress, 5); 
     if (address != null && address.size() > 0) { 
      Address location = address.get(0); 
      p1 = new LatLng(location.getLatitude(), location.getLongitude()); 
     } else { 
      p1 = new LatLng(19.111258, 72.908313); 
     } 

UPDATE:

你爲什麼不嘗試類似的東西:

public LatLng getLocationFromAddress(String strAddress) { 
    Geocoder coder = new Geocoder(this); 
    List<Address> address; 
    LatLng p1 = null; 
    try { 
    address = coder.getFromLocationName(strAddress, 5); 
     if (address != null && address.size() > 0) { 
      Address location = address.get(0); 
      p1 = new LatLng(location.getLatitude(), location.getLongitude()); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    if (p1 == null) { 
     p1 = new LatLng(19.111258, 72.908313); 
    } 
    return p1; 
} 
+0

Ive編輯了一下這個問題。修改了這個修改。同名的問題,不同的錯誤雖然 – Androider 2014-09-03 11:35:27

+0

204行代碼是什麼? – 2014-09-03 11:37:50

+0

已對此進行了評論,它與map.addMarker(新MarkerOptions()。position(REST).title(restName))一致) – Androider 2014-09-03 12:20:40