【问题标题】:Android: transferring Context and activity from a fragment to a classAndroid:将上下文和活动从片段转移到类
【发布时间】:2017-04-10 11:02:19
【问题描述】:

我正在尝试将图像显示到图像视图中。在主要活动上它有效。 我想创建一个导航抽屉,所以我创建了一个片段。 为了显示我的图像,我使用了一个类EdtTraitment 和方法show()。此方法尝试搜索目录中的文件。为此,我需要主要活动的Context,但是正如您将看到的,当我尝试从片段中的EdtTraintement 类中获取此上下文时出现错误。

片段代码:

public class JourFragment extends Fragment {


public JourFragment() {
    // Required empty public constructor
}
Activity mActivity =  getActivity();
Context mContext = getActivity();
View view = null;
EdtTraitement edt = new EdtTraitement(mContext,mActivity);
TouchImageView img = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_jour, container, false);

    new Thread(new Runnable() {
        public void run() {
            img = (TouchImageView) view.findViewById(R.id.image_emplois);


            edt.showEdt("URL","images.png","URL","text.txt",img); //diplay the img

        }
    }).start();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    return view;
}}

EdtTraitement:

public class EdtTraitement { //constructeur

protected Context mContext;
protected Activity activity;
protected TouchImageView image_edt = null;
protected Bitmap bitmap = null;
public  EdtTraitement(Context mContext,Activity _activity) {

    activity =  _activity;
    this.mContext = mContext;
}


public void showEdt(String url_image,String name_img ,String url_md5,String name_md5, TouchImageView img){
    image_edt = img;

    File img_file = new File(mContext.getFilesDir(), name_img);
    if(isOnline()) {
        if ((getMd5(url_md5).equals(readFile(name_md5))) && (img_file.exists())) {
            image_edt.setImageBitmap(BitmapFactory.decodeFile(img_file.getPath()));
        } else {
            try {
                InputStream inputStream = null;
                inputStream = (InputStream) new URL(url_image).getContent();
                bitmap = BitmapFactory.decodeStream(inputStream);
                image_edt.setImageBitmap(bitmap);
                //on le stoque:
                storeImage(bitmap);

            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }else{

        image_edt.setImageBitmap(BitmapFactory.decodeFile(img_file.getPath()));
    }
    image_edt.setZoom(image_edt.getMaxZoom());
    image_edt.setMaxZoom(image_edt.getMaxZoom());
    image_edt.setMinZoom(image_edt.getMinZoom());
}}

日志:

04-10 06:54:00.329 11605-11780/oytoch.iut_info E/AndroidRuntime: FATAL EXCEPTION: Thread-4
                                                             Process: oytoch.iut_info, PID: 11605
                                                             java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference
                                                                 at oytoch.iut_info.EdtTraitement.showEdt(EdtTraitement.java:49)
                                                                 at oytoch.iut_info.JourFragment$1.run(JourFragment.java:38)
                                                                 at java.lang.Thread.run(Thread.java:761)

第 49 行:

 File img_file = new File(mContext.getFilesDir(), name_img);

它在没有片段的情况下工作我在类的其他部分代码中需要这个上下文。

我一直无法找到可行的解决方案;如何更改我的代码以获得我想要的结果?

【问题讨论】:

    标签: java android class android-fragments android-activity


    【解决方案1】:

    Fragment被创建时,getActivity()方法返回null。 您必须将 EdtTraitement 的创建更改为另一种方法(当活动已经创建时),如下所示:

     @Override
            public void onActivityCreated(@Nullable Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
            EdtTraitement edt = new EdtTraitement(mContext,mActivity);
            }
    

    另外,请记住,Activity 类是 Context,因此不必传递这两个参数,因为它是多余的。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多