Я занимаюсь разработкой приложения, которое может находить близлежащие места и отображать данные в настраиваемом представлении списка через текущее местоположение. Но я получаю сообщение об ошибке.
package com.sherry.hp.googlemap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Places1 {
public List<HashMap<String, String>> parse(JSONObject jsonObject) {
JSONArray jsonArray = null;
try {
jsonArray = jsonObject.getJSONArray("results");
} catch (JSONException e) {
e.printStackTrace();
}
return getPlaces(jsonArray);
}
private List<HashMap<String, String>> getPlaces(JSONArray jsonArray) {
int placesCount = jsonArray.length();
List<HashMap<String, String>> placesList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> placeMap = null;
for (int i = 0; i < placesCount; i++) {
try {
placeMap = getPlace((JSONObject) jsonArray.get(i));
placesList.add(placeMap);
} catch (JSONException e) {
e.printStackTrace();
}
}
return placesList;
}
private HashMap<String, String> getPlace(JSONObject googlePlaceJson) {
HashMap<String, String> googlePlaceMap = new HashMap<String, String>();
String placeName = "";
String vicinity = "";
String latitude = "";
String longitude = "";
String reference = "";
String pic = "";
try {
if (!googlePlaceJson.isNull("name")) {
placeName = googlePlaceJson.getString("name");
}
if (!googlePlaceJson.isNull("vicinity")) {
vicinity = googlePlaceJson.getString("vicinity");
}
latitude = googlePlaceJson.getJSONObject("geometry").getJSONObject("location").getString("lat");
longitude = googlePlaceJson.getJSONObject("geometry").getJSONObject("location").getString("lng");
reference = googlePlaceJson.getString("reference");
googlePlaceMap.put("place_name", placeName);
googlePlaceMap.put("vicinity", vicinity);
googlePlaceMap.put("lat", latitude);
googlePlaceMap.put("lng", longitude);
googlePlaceMap.put("reference", reference);
} catch (JSONException e) {
e.printStackTrace();
}
return googlePlaceMap;
}
}
Это мой класс, где я передаю ответ текущего местоположения.
Здесь я получаю свой ответ и сохраняю в Arraylist
public class PlacesDisplayTask extends AsyncTask<Object, Integer, List<HashMap<String, String>>> {
private JSONObject googlePlacesJson;
private GoogleMap googleMap;
ArrayList<Bean>list1;
@Override
protected List<HashMap<String, String>> doInBackground(Object... inputObj) {
list1 = new ArrayList<>();
List<HashMap<String, String>> googlePlacesList = null;
Places1 places1 = new Places1();
try {
googleMap = (GoogleMap) inputObj[0];
googlePlacesJson = new JSONObject((String) inputObj[1]);
googlePlacesList = places1.parse(googlePlacesJson);
} catch (Exception e) {
Log.d("Places1", e.toString()+"PlacesDisplayTask");
}
return googlePlacesList;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onPostExecute(List<HashMap<String, String>> list) {
for (int i = 0; i < list.size(); i++) {
MarkerOptions markerOptions = new MarkerOptions();
HashMap<String, String> googlePlace = list.get(i);
double lat = Double.parseDouble(Objects.requireNonNull(googlePlace.get("lat")));
double lng = Double.parseDouble(Objects.requireNonNull(googlePlace.get("lng")));
String placeName = googlePlace.get("place_name");
String vicinity = googlePlace.get("vicinity");
Log.e("TAG","vicinity"+vicinity);
list1.add(new Bean(lat,lng,placeName,vicinity));
LatLng latLng = new LatLng(lat, lng);
markerOptions.position(latLng);
markerOptions.title(placeName + " : " + vicinity);
googleMap.addMarker(markerOptions);
}
}
}
и после установки Пользовательского списка в класс я получил следующую ошибку
Это мой класс, в котором я установил Адаптер
public class RestaurantInfo extends AppCompatActivity {
String getUrl;
TextView textView;
ListView listView;
ArrayList<Bean>list;
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant_info);
textView = findViewById(R.id.textView);
listView = findViewById(R.id.listView);
Adapter adapter = new Adapter(RestaurantInfo.this,list);
listView.setAdapter(adapter);
}
и это мой собственный класс адаптера списка
public class Adapter extends BaseAdapter {
Context context;
ArrayList<Bean>list;
public Adapter(Context context, ArrayList<Bean> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ImageView imageView;
TextView name;
if(view == null)
{
LayoutInflater lf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view = lf.inflate(R.layout.custom,null);
}
name = view.findViewById(R.id.name);
imageView = view.findViewById(R.id.imageView);
name.setText(list.get(i).getPlaceName());
return view;
}
} * * тысяча двадцать-один
и я получил следующую ошибку, это мой logcat
Process: com.sherry.hp.googlemap, PID: 23247
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sherry.hp.googlemap/com.sherry.hp.googlemap.RestaurantInfo}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at com.sherry.hp.googlemap.Adapter.getCount(Adapter.java:27)
at android.widget.ListView.setAdapter(ListView.java:613)
at com.sherry.hp.googlemap.RestaurantInfo.onCreate(RestaurantInfo.java:42)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)