2017-04-10 4 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を返します。 あなたは、このような別の方法(活動はすでにcreateadあるとき)にごEdtTraitementの作成を変更する必要がありますので、必要なパスは、これらの2つのパラメータではありません、ActivityクラスはContextで、覚えて、また

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

それは冗長であるからです。

+0

ありがとうございます。それはうまくいきます、私はいつも答えの速さに非常に感銘を受けます。 このような問題を解決しようとしている他の人のために、この投稿は私に役立ちましたhttp://stackoverflow.com/questions/28929637/difference-and-uses-of-oncreate-oncreateview-and-onactivitycreated-in-fra –

+0

you'再歓迎@トーマスシャトリアー:D –

関連する問題