Android google 地图自定义信息 window 错误突然关闭
Android google map custom info window error close abruptly
我正在尝试使用此代码为我的标记创建自定义信息 window。我希望信息永久显示,我还想根据特定逻辑动态更改信息的图像 window。我从我的 google 搜索中改编了一些代码,如下所示。
public static MapActivityFragment1 newInstance(String param1, String param2) {
MapActivityFragment1 fragment = new MapActivityFragment1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map_activity_fragment1, null, false);
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
tv = (TextView) view.findViewById(R.id.textRefresh);
customHandler = new android.os.Handler();
customHandler.postDelayed(updateTimerThread, 0);
mMap.setInfoWindowAdapter(new BalloonAdapter(getLayoutInflater(savedInstanceState)));
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
private class Getdata extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
//pDialog = new ProgressDialog(MyMapActivity.this);
//pDialog.setMessage("Please wait...");
//pDialog.setCancelable(false);
//pDialog.show();
}
@Override
protected String doInBackground(String... params) {
try {
String fID= params[0];
URL url = new URL("http://******.php");
JSONObject postDataParams = new JSONObject();
postDataParams.put("fID", fID);
Log.e("params", postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
else {
return new String("false : "+responseCode);
}
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
String s = result.trim();
//Toast.makeText(getApplicationContext(), "restult is"+s, Toast.LENGTH_LONG).show();
String stringSucess = "";
int height = 50;
int width = 40;
BitmapDrawable bitmapdraw=(BitmapDrawable)getResources().getDrawable(R.drawable.basgray);
Bitmap b=bitmapdraw.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
//setListAdapter(adapter);
}
}
这是我的 BallonAdapter 的代码
public class BalloonAdapter implements InfoWindowAdapter {
LayoutInflater inflater = null;
private TextView textViewTitle;
public BalloonAdapter(LayoutInflater inflater) {
this.inflater = inflater;
}
@Override
public View getInfoWindow(Marker marker) {
View v = inflater.inflate(R.layout.balloon, null);
if (marker != null) {
textViewTitle = (TextView) v.findViewById(R.id.textViewTitle);
textViewTitle.setText(marker.getTitle());
}
return (v);
}
@Override
public View getInfoContents(Marker marker) {
return (null);
}
}
当我登录并来到这个片段时,应用程序就停止了。我在哪里可以搜索场景中的日志,或者我必须单独记录它,因为我也没有看到任何登录到 Android 工作室的东西。问题出在这一行 mMap.setInfoWindowAdapter(new BalloonAdapter(getLayoutInflater(savedInstanceState)));在 public 查看 onCreateView 函数中。
为了帮助其他人,我找到了我刚刚添加的解决方案 mMap.setInfoWindowAdapter(new BalloonAdapter(getLayoutInflater(null)));
return 查看;
}
意思是我用 null 这个词替换它现在正在工作。 IT 现在看起来运行良好。
我正在尝试使用此代码为我的标记创建自定义信息 window。我希望信息永久显示,我还想根据特定逻辑动态更改信息的图像 window。我从我的 google 搜索中改编了一些代码,如下所示。
public static MapActivityFragment1 newInstance(String param1, String param2) {
MapActivityFragment1 fragment = new MapActivityFragment1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map_activity_fragment1, null, false);
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
tv = (TextView) view.findViewById(R.id.textRefresh);
customHandler = new android.os.Handler();
customHandler.postDelayed(updateTimerThread, 0);
mMap.setInfoWindowAdapter(new BalloonAdapter(getLayoutInflater(savedInstanceState)));
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
private class Getdata extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
//pDialog = new ProgressDialog(MyMapActivity.this);
//pDialog.setMessage("Please wait...");
//pDialog.setCancelable(false);
//pDialog.show();
}
@Override
protected String doInBackground(String... params) {
try {
String fID= params[0];
URL url = new URL("http://******.php");
JSONObject postDataParams = new JSONObject();
postDataParams.put("fID", fID);
Log.e("params", postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
else {
return new String("false : "+responseCode);
}
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
String s = result.trim();
//Toast.makeText(getApplicationContext(), "restult is"+s, Toast.LENGTH_LONG).show();
String stringSucess = "";
int height = 50;
int width = 40;
BitmapDrawable bitmapdraw=(BitmapDrawable)getResources().getDrawable(R.drawable.basgray);
Bitmap b=bitmapdraw.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
//setListAdapter(adapter);
}
}
这是我的 BallonAdapter 的代码
public class BalloonAdapter implements InfoWindowAdapter {
LayoutInflater inflater = null;
private TextView textViewTitle;
public BalloonAdapter(LayoutInflater inflater) {
this.inflater = inflater;
}
@Override
public View getInfoWindow(Marker marker) {
View v = inflater.inflate(R.layout.balloon, null);
if (marker != null) {
textViewTitle = (TextView) v.findViewById(R.id.textViewTitle);
textViewTitle.setText(marker.getTitle());
}
return (v);
}
@Override
public View getInfoContents(Marker marker) {
return (null);
}
}
当我登录并来到这个片段时,应用程序就停止了。我在哪里可以搜索场景中的日志,或者我必须单独记录它,因为我也没有看到任何登录到 Android 工作室的东西。问题出在这一行 mMap.setInfoWindowAdapter(new BalloonAdapter(getLayoutInflater(savedInstanceState)));在 public 查看 onCreateView 函数中。
为了帮助其他人,我找到了我刚刚添加的解决方案 mMap.setInfoWindowAdapter(new BalloonAdapter(getLayoutInflater(null))); return 查看; } 意思是我用 null 这个词替换它现在正在工作。 IT 现在看起来运行良好。