Android谷歌地图自定义信息窗口错误突然关闭

时间:2017-02-19 08:54:50

标签: android google-maps infowindow

我正在尝试使用此代码为我的标记创建自定义信息窗口。我希望信息永久显示,我也想根据某些逻辑动态更改信息窗口的图像。我已经调整了谷歌搜索中的一些代码,如下所示。

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)));在公共视图onCreateView函数。

1 个答案:

答案 0 :(得分:0)

为了帮助其他我找到了解决方案,我刚刚添加了这个mMap.setInfoWindowAdapter(new BalloonAdapter(getLayoutInflater(null)));         返回视图;     } 意思是我用null替换它现在正在工作。 IT看起来效果很好。