2017-02-19 65 views
0

我試着用這段代碼爲我的標記創建自定義信息窗口。我希望信息永久出現,並且我也想根據某些邏輯動態地更改信息窗口的圖像。我已經從我的谷歌搜索中調整了一些代碼,如下所示。Android谷歌地圖自定義信息窗口錯誤突然關閉

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 Studio的內容。問題出現在這一行mMap.setInfoWindowAdapter(new BalloonAdapter(getLayoutInflater(savedInstanceState)));在公共視圖中查看onCreateView函數。

回答

0

爲了幫助其他人找到解決方案,我剛剛添加了這個mMap.setInfoWindowAdapter(new BalloonAdapter(getLayoutInflater(null))); 返回視圖; } 含義我用null替換它,現在它的工作。 IT現在看起來很好。