MapFragment崩溃不一致

时间:2014-09-03 10:28:40

标签: android google-maps parse-platform google-maps-markers mapfragment

以下活动从我的解析数据库中获取数据并显示它。我有一个函数getLocationFromAddress()来将字符串地址转换为LatLng格式。然后函数prepareMap()创建一个标记,然后指向地图上的该位置。活动的代码(不显示非imp的函数)如下

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)

绝对不知道这个问题是什么。我无法理解逻辑问题是什么,因为它似乎适用于某个餐厅。任何想法?编辑在评论中提出了快速更改。但问题仍然存在,但只有错误发生了变化

1 个答案:

答案 0 :(得分: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);
        }

<强>更新

为什么不尝试这样的事情:

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