【问题标题】:LatLang through Json NullPointer经纬度通过 Json 空指针
【发布时间】:2016-04-29 06:52:40
【问题描述】:

我通过 Json 收到了 LatLang,但现在我想在 Postexecute 方法中的地图上显示这是我的代码

 ServiceHandler serviceClient = new ServiceHandler();
        Log.d("url: ", "> " + URL_ITEMS);
        String json = serviceClient.makeServiceCall(URL_ITEMS, ServiceHandler.GET);
        // print the json response in the log
        Log.d("Get match fixture resps", "> " + json);
        if (json != null) {
            try {
                Log.d("try", "in the try");
                JSONObject jsonObj = new JSONObject(json);
                Log.d("jsonObject", "new json Object");
                // Getting JSON Array node
                matchFixture = jsonObj.getJSONArray(TAG_FIXTURE);
                Log.d("json aray", "user point array");
                int len = matchFixture.length();
                Log.d("len", "get array length");
                for (int i = 0; i < matchFixture.length(); i++) {
                    JSONObject c = matchFixture.getJSONObject(i);
                    Double matchId = Double.parseDouble(c.getString(TAG_MATCHID));
                    Log.d("matchId", String.valueOf(matchId));
                    Double teamA = Double.valueOf(c.getString(TAG_TEAMA));
                    Log.d("teamA", String.valueOf(teamA));

                    //  hashmap for single match
                    HashMap<String, String> matchFixture = new HashMap<String, String>();
                    // adding each child node to HashMap key => value
                    matchFixture.put(TAG_MATCHID, String.valueOf(matchId));
                    matchFixture.put(TAG_TEAMA, String.valueOf(teamA));

                    matchFixtureList.add(new LatLng(matchId, teamA));


                }
            } catch (JSONException e) {
                Log.d("catch", "in the catch");
                e.printStackTrace();
            }
        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }
        return null;
    }


    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        for (LatLng point:matchFixtureList){
            mMap.addMarker(new MarkerOptions().position(point).title("chexk"));
        }

        // mMap.addMarker(new MarkerOptions().position(pakistan).title("Marker in pakistn"));
        //mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(16));
        }

那么我如何在地图上显示标记,我在日志中收到所有纬度和纬度,但它在 logcatdfdfd 中显示空指针

【问题讨论】:

  • 如果提供的纬度经度是硬编码的,你会收到错误吗?还是显示正常?
  • 如果你的matchFixtureList 是一个全局变量,我认为你不需要改变任何东西。但最好遵循@ccsnoopy 的回答。
  • 先生,我只想根据我通过 JSON 收到的 LatLang 显示标记
  • 我认为您正在寻找类似 @​​987654321@ 的东西,希望对您有所帮助!

标签: android json google-maps


【解决方案1】:

您在(我假设是)doInBackground() 方法中返回了 null

尝试将您的 doInBackground 更改为:

@Override
protected List<LatLng> doInBackground(Someparam... params){
    List<LatLng> matchFixtureList = new ArrayList<>();
    //rest of your code

    return matchFixtureList;
}

@Override
protected void onPostExecute(List<LatLng> result){
    for (LatLng point: result){//access your result from doInBackground
        mMap.addMarker(new MarkerOptions().position(point).title("chexk"));
    }
   //the rest of your code
}

【讨论】:

  • 先生现在应用程序不会崩溃但不显示标记
  • 我不知道你的 mMap 是如何工作的,但是根据目前提供的信息,很遗憾我无能为力。
【解决方案2】:

检查教程中的所有步骤。交叉检查您是否遵循了所有内容。 http://wptrafficanalyzer.in/blog/adding-marker-at-user-input-latitude-and-longitude-in-google-map-android-api-v2/

对 Keystore 部分施加更多压力。你用的是同一个还是其他的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 2020-07-19
    相关资源
    最近更新 更多