2017-10-04 67 views
0

我想投SlideshowDialogFragment上下文在我的AsyncTask在DdownloadTask.java但是當我寫如何將片段投射到異步構造函數?

 final DownloadTask downloadTask = new 
     DownloadTask(myActivity.this); 

SlideshowDialogFragment代替myActivity,安卓顯示警告說 warning android

我不知道我是什麼我做 ?? THX的幫助我

public class SlideshowDialogFragment extends DialogFragment{ 


ArrayList<Image> images; 
ViewPager viewPager; 
MyViewPagerAdapter myViewPagerAdapter; 
TextView lblCount,lblTitle,lblDate; 
Button btn_set; 
Button btn_download; 
int selectedPostition; 
DownloadManager downloadManager; 
public static ProgressDialog mProgressDialog; 






static SlideshowDialogFragment newInstance(){ 
    SlideshowDialogFragment f=new SlideshowDialogFragment(); 
    return f; 
} 

@Override 
public View onCreateView (LayoutInflater inflater,ViewGroup container,Bundle saveInstanceState) 
{ 


    View v=inflater.inflate(R.layout.fragment_image_slider,container,false); 
    viewPager=(ViewPager)v.findViewById(R.id.view_pager); 

    lblTitle=(TextView)v.findViewById(R.id.title); 
    lblDate=(TextView)v.findViewById(R.id.date); 
    btn_set=(Button)v.findViewById(R.id.btn_set); 
    btn_download=(Button)v.findViewById(R.id.btn_download); 






    images=(ArrayList<Image>) getArguments().getSerializable("images"); 
    selectedPostition=getArguments().getInt("position"); 


    myViewPagerAdapter=new MyViewPagerAdapter(); 
    viewPager.setAdapter(myViewPagerAdapter); 



    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int 
     positionOffsetPixels) { 
      displayInfo(position); 
      //setWallpaper(position); 
     } 

     @Override 
     public void onPageSelected(int position) { 

     } 

     @Override 
     public void onPageScrollStateChanged(int state) { 

     } 
    }); 






    setCurrentItem(selectedPostition); 

    btn_download.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 


      download(selectedPostition); 



     } 
    }); 






    return v; 



} 

    void download (int position){ 

    Image image=images.get(position); 
    String large = image.getlarge(); 

    final DownloadTask downloadTask = new 
    DownloadTask(**SlideshowDialogFragment**.this); 
    downloadTask.execute(large); 
} 

,這是DownloadTask活動與構造我寫的AsyncTask在這個類:

public class DownloadTask extends AsyncTask<String, Integer, String> { 

private Context context; 
private PowerManager.WakeLock mWakeLock; 

public DownloadTask(Context context) { 
this.context = context; 
} 





@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    // take CPU lock to prevent CPU from going off if the user 
    // presses the power button during download 
    PowerManager pm = (PowerManager) 
    context.getSystemService(Context.POWER_SERVICE); 
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
    getClass().getName()); 
    mWakeLock.acquire(); 
    mProgressDialog.show(); 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) { 
    super.onProgressUpdate(progress); 
    // if we get here, length is known, now set indeterminate to false 
    mProgressDialog.setIndeterminate(false); 
    mProgressDialog.setMax(100); 
    mProgressDialog.setProgress(progress[0]); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
    mWakeLock.release(); 
    mProgressDialog.dismiss(); 
    if (result != null) 
    Toast.makeText(context,"خطای دانلود "+result, Toast.LENGTH_LONG).show(); 
    else 
    Toast.makeText(context,"دانلود با موفقیت انجام شد", 
    Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    protected String doInBackground(String... sUrl) { 
    InputStream input = null; 
    OutputStream output = null; 
    HttpURLConnection connection = null; 
    try { 
    URL url = new URL(sUrl[0]); 
    connection = (HttpURLConnection) url.openConnection(); 
    connection.connect(); 

    // expect HTTP 200 OK, so we don't mistakenly save error report 
    // instead of the file 
    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
    return "Server returned HTTP " + connection.getResponseCode() 
     + " " + connection.getResponseMessage(); 
    } 

    // this will be useful to display download percentage 
    // might be -1: server did not report the length 
    int fileLength = connection.getContentLength(); 

    // download the file 
    input = connection.getInputStream(); 
    output = new FileOutputStream("/sdcard/tabriz.jpg"); 

    byte data[] = new byte[4096]; 
    long total = 0; 
    int count; 
    while ((count = input.read(data)) != -1) { 
    // allow canceling with back button 
    if (isCancelled()) { 
     input.close(); 
     return null; 
    } 
    total += count; 
    // publishing the progress.... 
    if (fileLength > 0) // only if total length is known 
     publishProgress((int) (total * 100/fileLength)); 
    output.write(data, 0, count); 
    } 
} catch (Exception e) { 
    return e.toString(); 
} finally { 
    try { 
    if (output != null) 
     output.close(); 
    if (input != null) 
     input.close(); 
    } catch (IOException ignored) { 
    } 

    if (connection != null) 
    connection.disconnect(); 
} 
return null; 

}}

我是什麼鑄造SlideshowDialogFragment到異步嗎?

+0

如果你的問題是「如何讓一個片段內語境」,只需調用的getContext使用它()在你的片段中 – NSimon

+0

對於片段,你將上下文作爲getActivity()傳遞; new DownloadTask(getActivity()); –

+0

@AshwiniKutre我寫 final DownloadTask downloadTask = new DownloadTask(getActivity()); downloadTask.execute(large);並警告隱藏,但當我午餐申請和點擊按鈕中的應用程序片段崩潰 –

回答

0

你需要通過一個Context像你這樣定義你的構造函數。 A Fragment沒有Context,但Activity有。由於片段是一種活動的一部分,你可以用

SlideshowDialogFragment.this.getActivity(); 

訪問它,你也可以直接與DownloadTask(getActivity())

+0

我寫最終DownloadTask downloadTask = new DownloadTask(getActivity()); downloadTask.execute(大);並警告隱藏,但當我午餐申請和點擊按鈕中的碎片應用程序崩潰 –

+0

@MasihKolahdouzan什麼是崩潰日誌? –

+0

https://pasteboard.co/GNnCxtNS.png –