contextthemewrapper无法转换为活动 [英] contextthemewrapper cannot be cast to activity

查看:209
本文介绍了contextthemewrapper无法转换为活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的小型应用程序中,我有一个PageAdapter-> Fragment-> ListView-> ImageView.在onImageClick上,我想在另一个Fragment(Fragment3)中显示我的图像,但是当我在Fragment3中加载图像时,出现异常

in my small app i have a PageAdapter->Fragment->ListView->ImageView. On onImageClick i want display my image in another Fragment (Fragment3), but when i load an image in my Fragment3 get an Exception

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时,一切顺利,但是当我创建另一个片段(子级)并想要加载照片时,它会失败.

and code which creates an Exception. When i create a fragment and add the ListView all passed ok, but when i create another fragment (child) and want to load Photo it fails.

代码,我如何在没有PageAdapter的情况下创建子片段

code, how i create a child fragment, without 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线程,

here is Photo Loader Thread,

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)

推荐答案

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

您不能假设可以将Context强制转换为Activity.因此出现例外:您正在尝试将实际上是ContextThemeWrapperContext强制转换为Activity.

You can't assume a Context can be casted to an Activity. Hence the exception: You're attempting to cast a Context that is in fact a ContextThemeWrapper to an Activity.

您可以替换

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

带有例如

photoToLoad.imageView.post(bd);

Runnable发布到UI线程消息队列,类似于Activity runOnUiThread().

to post a Runnable to UI thread message queue, similar to Activity runOnUiThread().

这篇关于contextthemewrapper无法转换为活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆