0

I have a popup window on my fragment,i want to take drawing cache of popup window. I have tried this (below) but i got null pointer exception.I could'nt find the reason.Can anyone help?

 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>

I got NPE on this line

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

exception

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)
Jack
  • 1,825
  • 3
  • 26
  • 43

2 Answers2

1

NullPointerException is thrown when an application attempts to use an object reference that has the null value.

At first, Make sure your Imageview is not Empty .

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

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

Solution 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">

Solution 2

Your card_main_layout is empty . Try with ImageView

ImageView background_imageObj= (ImageView) customView_2.findViewById(R.id.background_image);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    Hai ,Thank you.I have tried this but Bitmap image = img_Obj.getDrawingCache(); , this bitmap is null . – Jack Feb 06 '17 at 09:52
  • http://stackoverflow.com/questions/22610699/getdrawingcache-always-returns-the-same-bitmap – IntelliJ Amiya Feb 06 '17 at 10:06
  • :sorry for the late response.I have solved this issue – Jack Feb 07 '17 at 08:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135029/discussion-between-gibs-and-intellij-amiya). – Jack Feb 07 '17 at 08:08
0

Below I have created one popup window with method on Button click of popup window its create bitmap from popup window please check below method and XML

method: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>

To show created bitmap I used one image view in Main Layout.

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43