【问题标题】:MapFragment crashes inconsistentlyMapFragment 不一致地崩溃
【发布时间】:2014-09-04 00:35:32
【问题描述】:

以下活动从我的解析数据库中获取数据并显示它。我有一个函数 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));
}

最搞笑的是上面的activity并不是每次都崩溃。它似乎只对某些餐厅崩溃。错误日志显示如下

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)

完全不知道是什么问题。我无法理解逻辑问题是什么,因为它似乎适用于某些餐厅。有什么想法吗?编辑在 cmets 中建议进行快速更改。但问题依然存在,只是错误发生了变化

【问题讨论】:

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


    【解决方案1】:

    您的问题可能出在这段代码中:

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

    【讨论】:

    • 我稍微编辑了问题。合并了更改。同样的问题,但不同的错误
    • 已评论,与 map.addMarker(new MarkerOptions().position(REST).title(restName)) 一致
    • 您解析到此 Marker custructor 的参数之一为空。进行快速调试以查找它是什么。我怀疑你在“restName”参数上没有得到任何东西。
    • 我完全删除了 restName 部分,它仍然返回相同的错误。只有其他选项似乎是 REST,但这是不可能的
    • 你能测试一下你的 REST 对象总是被初始化吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多