2017-04-10 22 views
0

我試圖在圖像視圖中顯示圖像。關於它的主要活動。 我想創建一個導航抽屜,所以我創建了一個片段。 要顯示我的圖像我使用類EdtTraitment和方法show()。此方法嘗試搜索目錄中的文件。爲此,我需要主要活動的Context,但正如您將看到的,在嘗試從片段中獲得EdtTraintement類中的此上下文時出現錯誤。Android:將上下文和活動從片段傳輸到類

片段代碼:

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); 

它在沒有片段的情況下工作我在類的其他部分代碼中需要此上下文。

我一直無法找到工作解決方案;我如何更改我的代碼以獲得我想要的結果?

回答

0

當創建Fragment時,getActivity()方法返回null。 你有你的EdtTraitement的創建改變爲另一種方法(當活動已經createad)是這樣的:

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

此外,請記住,在Activity類是Context,所以沒有必要通過這兩個參數,因爲它是多餘的。

+0

謝謝你。它的工作原理,我總是對答案的速度印象深刻。 對於其他誰試圖解決這樣的問題,這篇文章幫助我http://stackoverflow.com/questions/28929637/difference-and-uses-of-oncreate-oncreateview-and-onactivitycreated-in-fra –

+0

你'歡迎@Thomaschatellier:D –

相關問題