2014-02-09 26 views
11

在我的小應用程序中我有一個PageAdapter-> Fragment-> ListView-> ImageView。在onImageClick我想在另一個片段(Fragment3)顯示我的形象,但是當我加載我Fragment3圖像獲取異常contextthemewrapper不能投到活動

public class Fragment3 extends DialogFragment { 
ImageLoader imageLoader; 
... 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    view = (ViewGroup) inflater.inflate(R.layout.fragment3_layout, container, false); 
    imageView = (ImageView) view.findViewById(R.id.imageViewFrag3); 
    detail = (TextView) view.findViewById(R.id.textViewFrag3); 
    imageUrl = getArguments().getString("URL"); 
    imageView.setTag(imageUrl); 
    activity=this;  
    imageLoader = new ImageLoader(activity.getActivity().getApplicationContext());  
    imageLoader.DisplayImage(imageUrl, (Activity) activity.getActivity(), imageView);  
    return view; 
} 
... 
} 

和代碼創建一個例外。當我創建一個片段,並添加ListView全部通過確定,但是當我創建另一個片段(子),並希望加載照片失敗。

碼,我如何創建一個子片段,沒有PageAdapter

holder.imgViewImage = (ImageView) vi.findViewById(R.id.imageView01); 
     holder.imgViewImage.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) {     
       android.support.v4.app.FragmentTransaction ft = activity.getFragmentManager().beginTransaction();     
       Fragment3 newFragment = Fragment3.newInstance();      
       Bundle bundle=new Bundle(); 
       bundle.putString("URL", holder.imgViewImage.getTag().toString()); 
       newFragment.setArguments(bundle); 
       newFragment.show(ft, "dialog"); 
       Log.i("!!!", "Click on image");     
      } 
     }) 

這裏的Photo Loader主題,

class PhotosLoader extends Thread { 
    public void run() { 
     try { 
      while(true) 
      { 
       if(photosQueue.photosToLoad.size()==0) 
        synchronized(photosQueue.photosToLoad){ 
         photosQueue.photosToLoad.wait(); 
        } 
       if(photosQueue.photosToLoad.size()!=0) 
       { 
        PhotoToLoad photoToLoad; 
        synchronized(photosQueue.photosToLoad){ 
         photoToLoad=photosQueue.photosToLoad.pop(); 
        } 
        Bitmap bmp=getBitmap(photoToLoad.url); 
        cache.put(photoToLoad.url, bmp); 
        if(((String)photoToLoad.imageView.getTag()).equals(photoToLoad.url)){ 
         BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad.imageView);    
         //Exception contextthemewrapper cannot be cast to activity !!! 
         Activity a= (Activity)(photoToLoad.imageView.getContext()); 
         a.runOnUiThread(bd); 
        } 
       } 
       if(Thread.interrupted()) 
        break; 
      } 
     } catch (InterruptedException e) { 
     } 
    } 
} 

和異常

02-09 08:22:57.510: E/AndroidRuntime(1449): FATAL EXCEPTION: Thread-114 
02-09 08:22:57.510: E/AndroidRuntime(1449): java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity 
02-09 08:22:57.510: E/AndroidRuntime(1449):  at com.example.ids.ImageLoader$PhotosLoader.run(ImageLoader.java:171) 
+0

這裏是這段代碼Activity a =(Activity)photoToLoad.imageView.getContext(); ? – keshav

回答

11
Activity a= (Activity)(photoToLoad.imageView.getContext()); 

你可以用」假設一個Context可以被鑄造成Activity。因此,例外情況是:您試圖將Context實際上是ContextThemeWrapper轉換爲Activity

可以更換

Activity a= (Activity)(photoToLoad.imageView.getContext()); 
a.runOnUiThread(bd); 

與例如

photoToLoad.imageView.post(bd); 

來發布Runnable到UI線程的消息隊列,類似於ActivityrunOnUiThread()

+0

謝謝你的幫助。 – zond

55

類似的情況我有。 可能是相關問題>Accessing activity from custom button

我用這段代碼解決了我的情況。

private static Activity scanForActivity(Context cont) { 
    if (cont == null) 
     return null; 
    else if (cont instanceof Activity) 
     return (Activity)cont; 
    else if (cont instanceof ContextWrapper) 
     return scanForActivity(((ContextWrapper)cont).getBaseContext()); 

    return null; 
} 
+1

輝煌。完全是我所需要的,將「ContextThemeWrapper」「投射」到Activity。 –

+0

太棒了!應該被接受爲答案。 – MHogge

+0

謝謝!也適用於AppCompatActivity! – Herman

1

據我所知,如果是在一個對話框中顯示一個視圖,它的上下文(access從view.getContext())實際上是一個ThemeContextWrapper實例,這可能不是一個活動。要從對話框中獲取活動,可以使用getOwnerActivity(),它將返回該對話框所屬的活動。