for循环中的错误。 android应用程序

时间:2014-11-21 02:11:48

标签: android json eclipse

我还是这个节目的新手。我尝试将marker.get(" title")分配给title数组。如果我运行它,应用程序崩溃没有错误标记。谢谢你的帮助

编辑实际代码中没有**。我只想指出哪里出错。

@Override
  protected void onPostExecute(List<HashMap<String, String>> result) {
      for(int i=0; i<result.size();i++){
          HashMap<String, String> marker = result.get(i);
          LatLng latlng = new LatLng(Double.parseDouble(marker.get("lat")), Double.parseDouble(marker.get("lng")));
          PlaceName = marker.get("title");
          Address = marker.get("FullAddress");
          telno = marker.get("telno");
          addMarker(latlng, PlaceName, Address);

          **for (int x=0;x<=i;x++)
          {
              title[x]=marker.get("title");
          }**

      }
  }
}

logcat错误

11-21 10:16:09.273: E/AndroidRuntime(30994): FATAL EXCEPTION: main
11-21 10:16:09.273: E/AndroidRuntime(30994): Process: com.mha.katpenang, PID: 30994
11-21 10:16:09.273: E/AndroidRuntime(30994): java.lang.NullPointerException
11-21 10:16:09.273: E/AndroidRuntime(30994):    at com.mha.katpenang.MainActivity$ParserTask.onPostExecute(MainActivity.java:252)
11-21 10:16:09.273: E/AndroidRuntime(30994):    at com.mha.katpenang.MainActivity$ParserTask.onPostExecute(MainActivity.java:1)
11-21 10:16:09.273: E/AndroidRuntime(30994):    at android.os.AsyncTask.finish(AsyncTask.java:632)
11-21 10:16:09.273: E/AndroidRuntime(30994):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-21 10:16:09.273: E/AndroidRuntime(30994):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
11-21 10:16:09.273: E/AndroidRuntime(30994):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-21 10:16:09.273: E/AndroidRuntime(30994):    at android.os.Looper.loop(Looper.java:136)
11-21 10:16:09.273: E/AndroidRuntime(30994):    at android.app.ActivityThread.main(ActivityThread.java:5034)
11-21 10:16:09.273: E/AndroidRuntime(30994):    at java.lang.reflect.Method.invokeNative(Native Method)
11-21 10:16:09.273: E/AndroidRuntime(30994):    at java.lang.reflect.Method.invoke(Method.java:515)
11-21 10:16:09.273: E/AndroidRuntime(30994):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
11-21 10:16:09.273: E/AndroidRuntime(30994):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
11-21 10:16:09.273: E/AndroidRuntime(30994):    at dalvik.system.NativeStart.main(Native Method)

修正:需要在声明中设置标题变量大小。

2 个答案:

答案 0 :(得分:0)

检查您是如何定义数组title的。它应该是这样的:

String [] title = new String[whateverSize]; // with your chosen size

跳跃帮助

答案 1 :(得分:0)

我认为你必须在进行任何关于获取值的操作之前进行空检查

      LatLng latlng = new LatLng(Double.parseDouble(marker.get("lat")), Double.parseDouble(marker.get("lng")));

如果你喜欢它会更好: -

@Override
  protected void onPostExecute(List<HashMap<String, String>> result) {
      if(result != null){
        for(int i=0; i<result.size();i++){
          HashMap<String, String> marker = result.get(i);

          LatLng latlng = new LatLng(Double.parseDouble(marker.get("lat")!=null ?
          marker.get("lat") : "0.0"), Double.parseDouble(marker.get("lng") !=null?
          marker.get("lng") :"0.0"));

          PlaceName = marker.get("title");
          Address = marker.get("FullAddress");
          telno = marker.get("telno");
          addMarker(latlng, PlaceName, Address);

          **for (int x=0;x<=i;x++)
          {
              title[x]=marker.get("title");
          }**

      }
   }

}