2017-02-09 112 views
0

我試圖從回收站查看圖片請點擊打開一個對話框......對話框包含一個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)

回答

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; 
} 
+0

在不同的幻燈片中顯示不同的圖像... –

+0

好的:在Glide調用的.load方法中,寫下:「_imagePaths.get(position)」,你做總是「_imagePaths.get(0)」 –

+0

看到我的更新的答案 –

0

之前的對話框創建它沒有「所有者」。因此致電dialog.getOwnerActivity()返回null

只是一個提示:你不需要通過Activity到你的適配器。

在您instantiateItem只使用從容器中的上下文接收LayoutInflater:

LayoutInflater inflater = LayoutInflater.from(container.getContext()); 
+0

謝謝....這工作....但是,當我更改 imagePaths [0]此圖像路徑...它給出一個錯誤...當imagePaths [0]是滑塊顯示兩個圖像設置,但都是相同的圖像...何w在不同的幻燈片中可以有不同的圖像... –