Android:将上下文和活动从片段转移到类

时间:2017-04-10 11:02:20

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

我正在尝试将图像显示到图像视图中。关于它的主要活动。 我想创建一个导航抽屉,所以我创建了一个片段。 要显示我的图片,请使用课程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);

它不使用片段我需要在类的其他代码部分使用此上下文。

我一直找不到合适的解决方案;如何更改我的代码以获得我想要的结果?

1 个答案:

答案 0 :(得分:0)

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

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

另外,请记住,Activity类是Context,因此没有必要通过这两个参数,因为它是多余的。