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) 

Абсолютно не знаю, в чем проблема. Я не могу понять, что такое логическая проблема, потому что она работает для определенного ресторана. Все идеи? EDIT Сделано быстрое изменение, предложенное в комментариях. Но проблема сохраняется, но только ошибка изменилась

ответ

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

+0

какой код в строке 204? –

+0

Прокомментировал это, его строка с map.addMarker (новый MarkerOptions(). Position (REST) ​​.title (restName)) – Androider

 Смежные вопросы

  • Нет связанных вопросов^_^