我的片段上有一个弹出窗口,我想要弹出窗口的绘图缓存。 我试过这个(下面),但我得到空指针异常。我找不到原因。谁能帮忙?
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>
我在这条线上获得了NPE
Bitmap drawingCache_bitmap_card = Bitmap.createBitmap(card_main_layout.getDrawingCache());
例外
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)
答案 0 :(得分:1)
首先,确保您的Imageview不是空的。
img_Obj.buildDrawingCache();
Bitmap image = img_Obj.getDrawingCache();
阅读 createBitmap
createBitmap(Bitmap source, int x, int y, int width, int height)
问题
card_main_layout = (RelativeLayout) customView_2.findViewById(R.id.card_main_layout);
解决方案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">
解决方案2
您的 card_main_layout 为空。尝试使用ImageView
ImageView background_imageObj= (ImageView) customView_2.findViewById(R.id.background_image);
答案 1 :(得分:0)
下面我创建了一个带弹出窗口方法的弹出窗口,点击弹出窗口,弹出窗口创建位图,请查看下面的方法和XML
方法: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>
为了显示创建的位图,我在主布局中使用了一个图像视图。