2017-10-04 37 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 –