2017-02-06 2 views
0

Ich habe ein Popup-Fenster auf meinem Fragment, ich möchte Zeichnung Cache des Popup-Fensters nehmen. Ich habe versucht, dies (unten), aber ich habe null Zeiger Ausnahme.Ich konnte nicht den Grund finden.Kann jemand helfen?Wie bekomme ich den Cache eines Popup-Fensters?

LayoutInflater inflater2 = (LayoutInflater)   obj.getSystemService(LAYOUT_INFLATER_SERVICE); 
     View customView_2 = inflater2.inflate(R.layout.share_lc_card, null); 


     // results_page_ = (RelativeLayout) v.findViewById(R.id.fragment_results_layout); 
     card_main_layout = (RelativeLayout) customView_2.findViewById(R.id.card_main_layout); 

     card_main_layout.invalidate(); 
        card_main_layout.requestLayout(); 
        card_main_layout.setDrawingCacheEnabled(true); 
        card_main_layout.buildDrawingCache(true); 
        Bitmap drawingCache_bitmap_card = card_main_layout.getDrawingCache(); 
       // Bitmap drawingCache_bitmap_card = Bitmap.createBitmap(card_main_layout.getDrawingCache()); 
        card_main_layout.setDrawingCacheEnabled(false); 
        Log.e("gu", "hghh"); 


My popup window xml 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/card_main_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_width="300dp" 
    android:layout_height="450dp" 
    android:layout_margin="4dp" 
    android:padding="30dp" 
    card_view:cardCornerRadius="26dp" 
    card_view:cardElevation="10sp" 
    card_view:cardUseCompatPadding="true"> 

    <RelativeLayout 
     android:layout_width="300dp" 
     android:layout_height="450dp"> 

     <ImageView 
      android:id="@+id/background_image" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="centerCrop" 
      android:src="@drawable/lc_background" /> 

     <TextView 
      android:id="@+id/first_name_textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="80dp" 
      android:layout_marginStart="80dp" 
      android:layout_marginTop="30dp" 

      android:text="@string/romeo" /> 

     <TextView 
      android:id="@+id/and_symbol" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="130dp" 
      android:layout_marginStart="130dp" 
      android:layout_marginTop="30dp" 
      android:text="@string/and" /> 


     <TextView 
      android:id="@+id/second_name_textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="145dp" 
      android:layout_marginStart="145dp" 
      android:layout_marginTop="30dp" 
      android:text="@string/juliet" /> 

     <ImageView 
      android:id="@+id/small_heart_image_1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="90dp" 
      android:layout_marginStart="90dp" 
      android:layout_marginTop="50dp" 
      android:src="@drawable/heart_share_card" /> 

     <TextView 
      android:id="@+id/result_message" 
      android:layout_width="200dp" 
      android:layout_height="100dp" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="40dp" 
      android:layout_marginStart="40dp" 
      android:layout_marginTop="120dp" 
      android:gravity="center" 
      android:lineSpacingExtra="-2dp" 
      android:text="WOW ! MADE FOR EACH OTHER" 
      android:textSize="25dp" /> 

     <ImageView 
      android:id="@+id/small_heart_image_2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_marginBottom="100dp" 
      android:layout_marginLeft="90dp" 
      android:layout_marginStart="90dp" 
      android:src="@drawable/heart_share_card" /> 

     <TextView 
      android:id="@+id/score_textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_marginBottom="80dp" 
      android:layout_marginLeft="110dp" 
      android:layout_marginStart="110dp" 
      android:text="90/100" /> 
    </RelativeLayout> 


</android.support.v7.widget.CardView> 

Ich habe NPE auf dieser Linie

Bitmap drawingCache_bitmap_card = Bitmap.createBitmap(card_main_layout.getDrawingCache()); 

Ausnahme

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object refe at android.graphics.Bitmap.createBitmap(Bitmap.java:737) 
at tech.a.com.x.LC_Fragments.ResultsFragment$4.onClick(ResultsFragment.java:207) 
at android.view.View.performClick(View.java:5184) 
at android.view.View$PerformClick.run(View.java:20910) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5951) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

Antwort

0

NullPointerException wird ausgelöst, wenn eine Anwendung versucht, einen Objektverweis zu verwenden, die die Null hat Wert.

Stellen Sie zuerst sicher, dass Ihr Imageview nicht leer ist.

 img_Obj.buildDrawingCache(); 
     Bitmap image = img_Obj.getDrawingCache(); 

lesen createBitmap

createBitmap(Bitmap source, int x, int y, int width, int height) 

Problem

card_main_layout = (RelativeLayout) customView_2.findViewById(R.id.card_main_layout); 

Lösung 1

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/card_main_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/lc_background"> 

Lösung 2

Ihre card_main_layout ist leer. Versuchen Sie, mit Image

ImageView background_imageObj= (ImageView) customView_2.findViewById(R.id.background_image); 
+1

Hai, Danke. Ich habe es versucht, aber Bitmap image = img_Obj.getDrawingCache(); , diese Bitmap ist null. – Gibs

+0

http://stackoverflow.com/questions/22610699/getdrawingcache-always-returns-the-same-bitmap –

+0

@Gibs bitte teilen Sie Ihren vollständigen Code –

0

Im Folgenden werde ich ein Popup-Fenster mit der Methode auf Tastenklick von Popup-Fenster erstellt haben seine erstellen Bitmap aus Popup-Fenster überprüfen Sie bitte das folgende Verfahren und XML

Methode: java

PopupWindow popupWindow = null; 
Bitmap bitmap = null; 


private void popUp() { 
      LayoutInflater layoutInflater 
        = (LayoutInflater) getBaseContext() 
        .getSystemService(LAYOUT_INFLATER_SERVICE); 

      View popupView = layoutInflater.inflate(R.layout.popup_window, null); 
      popupWindow = new PopupWindow(
        popupView, 
        LinearLayout.LayoutParams.WRAP_CONTENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT); 

      Button btnDismiss = (Button) popupView.findViewById(R.id.dismiss); 
      btnDismiss.setOnClickListener(new Button.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        try { 
         View view = popupWindow.getContentView(); 
         view.setDrawingCacheEnabled(true); 
         view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW); 
         view.buildDrawingCache(); 
         bitmap = Bitmap.createBitmap(view.getDrawingCache()); 
         view.setDrawingCacheEnabled(false); 
         view.destroyDrawingCache(); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
        imageView.setImageBitmap(bitmap); 
        popupWindow.dismiss(); 
       } 
      }); 

      popupWindow.showAsDropDown(button, 50, -30); 
     } 

XML- popup_window.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/rootView" 
    android:background="@android:color/background_light" 
    android:orientation="vertical"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="1dp" 
     android:background="@android:color/darker_gray" 
     android:orientation="vertical"> 
     > 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="20dp" 
      android:orientation="vertical"> 
      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="It's a PopupWindow" /> 
      <ImageView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ic_menu_share" /> 
      <Button 
       android:id="@+id/dismiss" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Dismiss" /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

erstellt Bitmap Um zu zeigen, habe ich eine Bildansicht im Hauptlayout.

Verwandte Themen