2017-02-22 26 views
0

我在我的片段的第一個視圖中具有以下代碼,如下所示。Layoutinflater轉換爲位圖中的自定義Google標記錯誤

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); 

     return view; 
} 

接下來我運行一個計時器,並調用一個異步函數,其中在postexecute我有following.Here我做的是,我有以下標記圖標和文本框頂部的佈局,我需要動態填充。

protected void onPostExecute(String result) { 
      String s = result.trim(); 
      markers.clear(); 
      markersList.clear(); 
      mMap.clear(); 
      Log.e("onPostExecute ",result); 

      //Toast.makeText(getApplicationContext(), "restult is"+s, Toast.LENGTH_LONG).show(); 
      String stringSucess = ""; 
      //markers = new Hashtable<String, String>(); 
      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); 
      mCustomMarkerView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_info_window, null); 
      mMarkerImageView = (ImageView) mCustomMarkerView.findViewById(R.id.profile_image); 
      Bitmap bitmap=null; 
      Marker mk = mMap.addMarker(new MarkerOptions() 
        .position(new LatLng(Double.parseDouble("-34"),Double.parseDouble("151"))) 
        .icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(mCustomMarkerView,bitmap)))); 

      mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 

} 

我收到此錯誤消息。 .icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(mCustomMarkerView,bitmap))));

問題有時我不知道什麼是錯誤只有一個它我得到這個錯誤java.lang.NullPointerException:嘗試調用虛擬方法'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap )'的空對象引用,但其他時間沒有錯誤。爲什麼只有一次,但其餘的沒有錯誤,只是停了下來。可以做些什麼來解決這個問題,以及如何爲我膨脹的佈局設置動態文本?

下面是我的生成位圖圖像的函數。

private Bitmap getMarkerBitmapFromView(View view, Bitmap bitmap) { 
     mMarkerImageView.setImageBitmap(bitmap); 
     view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); 
     view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 
     view.buildDrawingCache(); 
     Bitmap returnedBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), 
       Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(returnedBitmap); 
     canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN); 
     Drawable drawable = view.getBackground(); 
     if (drawable != null) 
      drawable.draw(canvas); 
     view.draw(canvas); 
     return returnedBitmap; 
} 

這是自定義佈局文件之一。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:id="@+id/dialogimage2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="center" 
     android:weightSum="1"> 

     <TextView 
      android:id="@+id/textValue" 
      android:layout_width="wrap_content" 
      android:layout_height="30dp" 
      android:background="@drawable/dialog" 
      android:text="PKH 52363" 
      android:gravity="top|center" 
      android:textColor="#000000" 
      android:textSize="15dp" 
      android:padding="3dp" 
      android:layout_weight="0" /> 

     <ImageView 
      android:id="@+id/markerImage" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:layout_marginLeft="-10dp" 
      android:layout_marginTop="0dp" 
      android:src="@drawable/basgray" /> 
    </LinearLayout> 

</LinearLayout> 

這裏是如何看起來在地圖上。

enter image description here

這是怎麼看的設計。

enter image description here

+0

後'custom_info_window.xml' –

+0

@KishoreJethava我已經加入我的custom_infor_window.xml。我會爲後者提供各種不同的圖標和背景圖像。我會想讓他們動態的谷歌地圖標記,也動態地填充文本視圖。 – user5313398

回答

1

根據您的佈局文件custom_info_window。XML

應該

mMarkerImageView = (ImageView) mCustomMarkerView.findViewById(R.id.markerImage); 

,而不是

mMarkerImageView = (ImageView) mCustomMarkerView.findViewById(R.id.profile_image); 

您正在使用錯誤的ID來尋找查看

+0

對不起,我對這種充氣技術有點太新了,我已經把一些代碼放入測試並得到了測試。我想進一步瞭解我是否正確地使用自定義標記來擴充此自定義佈局。我會爲我的每一點做長期的工作。進一步如何更新我的文字android:id =「@ + id/textValue」? – user5313398

+0

首先你解決了以上'NPE'錯誤? –

+0

@給我幾分鐘我不知道爲什麼我的android工作室每次都花費很長時間在gradle上構建這個規範到處都需要幾分鐘的時間來編譯? – user5313398

1

無論錯誤您收到,現在你應該遵循一些步驟,以瞭解並使得代碼對於未來的情況下

工作
  • 第一:錯誤是在這條線mMarkerImageView.setImageBitmap(bitmap);

  • 秒:你應該在onCreateView中膨脹你的佈局,但不應該在onCreateView中使用findViewById來初始化其他視圖,因爲有時視圖沒有被正確地初始化。因此,總是在onViewCreated中使用findViewById(當視圖完全創建時)嘗試一下,然後讓我們看看它是如何發展的。

  • 第三種:檢查ID是否爲casting正確的視圖(findviewbyid ..)具有正確的類型轉換以及託管它的變量是否具有正確的類型。例如,ImageView會與ImageView ID一起使用,而不會與其他內容一起使用。

+0

我不明白爲什麼有時我的應用程序只是關閉,但沒有顯示錯誤,所以在這種情況下如何捕獲或我可以在哪裏看到錯誤?說如果我部署到某個電話去尋找崩潰的原因? – user5313398

+0

首先你提到了這個mMarkerImageView.setImageBitmap(bitmap);這條線上的錯誤是什麼我不明白你的意思? – user5313398

+0

第二我不能膨脹onCreateView原因onPostExecute(字符串結果)中的每個不同的經緯度長點我會有其他額外的邏輯來決定哪個佈局將膨脹導致接下來我要爲每個不同的標記創建更多的自定義佈局?那麼動態實現這一點的最佳機制是什麼? – user5313398