我正在尝试从Recycler View图像打开一个对话框单击...对话框包含一个Viewpager来制作一个刷卡视图库..
打开对话框的代码
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(holder.itemView.getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(true);
dialog.setContentView(R.layout.activity_fullscreen_image);
ImageView back=(ImageView)dialog.findViewById(R.id.back);
ViewPager viewPagerPhotoSlideShow = (ViewPager) dialog.findViewById(R.id.pager);
ArrayList<String> pagerListItems=new ArrayList<String>();
pagerListItems.add(0,"https://firebasestorage.googleapis.com/v0/b/zumi-60a8f.appspot.com/o/Chat_Images%2Fcropped-1222406361.png?alt=media&token=22b8673f-5602-48d4-980f-986d505d2d8f");
pagerListItems.add(1,"https://firebasestorage.googleapis.com/v0/b/zumi-60a8f.appspot.com/o/Chat_Images%2Fcropped1611377016.png?alt=media&token=e7a40c6c-35ad-48ee-80ca-656384fcf68c");
FullScreenImageAdapter adapterPhotoViewPager = new FullScreenImageAdapter(dialog.getOwnerActivity(), pagerListItems);
viewPagerPhotoSlideShow.setAdapter(adapterPhotoViewPager);
viewPagerPhotoSlideShow.setCurrentItem(0);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
});
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
lp.gravity = Gravity.CENTER;
Window window = dialog.getWindow();
window.setAttributes(lp);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
适配器代码:
public class FullScreenImageAdapter extends PagerAdapter {
private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
// constructor
public FullScreenImageAdapter(Activity activity,
ArrayList<String> imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}
@Override
public int getCount() {
return this._imagePaths.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgDisplay;
// Button btnClose;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image_each, container,
false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay);
Glide.with(container.getContext()).load(_imagePaths.get(0))
.priority(Priority.HIGH).animate(android.R.anim.fade_in)
.override(120, 87).fitCenter().skipMemoryCache(false)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.into(imgDisplay);
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
XML&gt;&gt;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/arrow"
android:rotation="180"
android:id="@+id/back"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
每个视图的XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imgDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter" />
</RelativeLayout>
错误&gt;&gt;&gt;
E / AndroidRuntime:致命异常:主要 处理:com.example.cosmic.zumi_test,PID:22289 java.lang.NullPointerException:尝试调用虚方法 &#39; java.lang.Object中 android.content.Context.getSystemService(java.lang.String中)&#39;在null 对象参考 在android.view.LayoutInflater.from(LayoutInflater.java:219) 在 com.example.cosmic.zumi_test.FullScreenImageAdapter.instantiateItem(FullScreenImageAdapter.java:56) 在android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1182) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1116) 在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642) 在android.view.View.measure(View.java:17572) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5604) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:722) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:613) 在android.view.View.measure(View.java:17572) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5604) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:436) 在android.view.View.measure(View.java:17572) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5604) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:436) 在android.view.View.measure(View.java:17572) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5604) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:436) 在 com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2632) 在android.view.View.measure(View.java:17572) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061) 在 android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5895) 在 android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767) 在android.view.Choreographer.doCallbacks(Choreographer.java:580) 在android.view.Choreographer.doFrame(Choreographer.java:550) 在 android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5345) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:947) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
答案 0 :(得分:0)
在创建对话框之前,它没有“所有者”。因此,调用dialog.getOwnerActivity()
会返回null
。
只是提示:您无需将Activity
传递到适配器。
在instantiateItem
中,只需使用容器中的Context来接收LayoutInflater:
LayoutInflater inflater = LayoutInflater.from(container.getContext());
答案 1 :(得分:0)
_activity
内的FullScreenImageAdapter
似乎为空。这是因为dialog.getOwnerActivity()
为空(如前所述@dipdipdip)。不要像你那样实例化视图寻呼机适配器,而是尝试这种方式:
FullScreenImageAdapter adapterPhotoViewPager = new FullScreenImageAdapter(v.getContext(), pagerListItems);
<强>更新强>
要在对话框中显示不同的图像,请以这种方式更改寻呼机适配器内的instantiateItem
方法:
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgDisplay;
// Button btnClose;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image_each, container,
false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay);
Glide.with(container.getContext()).load(_imagePaths.get(position))
.priority(Priority.HIGH).animate(android.R.anim.fade_in)
.override(120, 87).fitCenter().skipMemoryCache(false)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.into(imgDisplay);
((ViewPager) container).addView(viewLayout);
return viewLayout;
}