我試圖從回收站查看圖片請點擊打開一個對話框......對話框包含一個Viewpager使輕掃圖庫..錯誤對話框與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 >>
<?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>
錯誤>>>
E/AndroidRuntime:致命異常:主 工藝:com.example.cosmic.zumi_test,PID:22289 java.lang中.NullPointerException:嘗試在一個空 對象引用 調用虛擬方法 'java.lang.Object中 android.content.Context.getSystemService(java.lang.String中)' at android.view.LayoutInflater.from(LayoutInflater.java:219) at com.example.cosmic.zumi_test.FullScreenImageAdapter.instantiateItem(FullScreenImageAdapter.java:56) at android.support.v4.view.ViewPager.addNewItem( ViewPager.java:1034) at android.support.v4.view.ViewPager.populate(ViewPager.java:1182) at android.support.v4.view.ViewPager.populate(ViewPager.java:1116) at android。在android.view.View.Group.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) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5604) at android.widget.FrameLayout.onMeasure(FrameLayout.java:436) at android。 view.View.measure(View.java:17572) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5604) at android.widget.F rameLayout.onMeasure(FrameLayout.java:436) at android.view.View.measure(View.java:17572) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5604) at android.widget.FrameLayout。 onMeasure(FrameLayout.java:436) at com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2632) at android.view.View.measure(View.java:17572) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173) at android.view.ViewRootImpl。performTraversals(ViewRootImpl.java:1379) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061) 在 android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5895) 在 android.view。編排$ CallbackRecord.run(Choreographer.java:767) at android.view.Choreographer.doCallbacks(Choreographer.java:580) at android.view.Choreographer.doFrame(Choreographer.java:550) at android.view .Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5345) 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的:947) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
在不同的幻燈片中顯示不同的圖像... –
好的:在Glide調用的.load方法中,寫下:「_imagePaths.get(position)」,你做總是「_imagePaths.get(0)」 –
看到我的更新的答案 –