-1

I've previously used this for downloading the data from database. Now I'm trying to use the same in fragments in tabbed Activity but it showing error at at Context. I tried a lot about it but I couldn't find any solution I hope anyone could help me. Here this a screenshot of the error occurring:

error occuring

Fragment

package com.custardpine.v_guide.movie;

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.custardpine.v_guide.R;

public class Trending extends Fragment{

final String url = "http://192.168.43.51/proj/trending.php";
ListView lv;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_movie_trending, container, false);
    lv = (ListView) rootView.findViewById(R.id.mves_list);
    DownloaderMovies d= new DownloaderMovies(Trending.this,url,lv);
    d.execute();
    return rootView;
}
}

Downloader

package com.custardpine.v_guide.movie;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.ListView;
import android.widget.Toast;


import com.custardpine.v_guide.bus.Connector;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

public class DownloaderMovies extends AsyncTask<Void,Void,String> {

    Context c;
    String url;
    ListView lv;
    ProgressDialog pd;

public DownloaderMovies(Trending c, String url, ListView lv) {

    this.c = c;          <--Error Occuring Here-->
    this.url = url;
    this.lv = lv;

}


@Override
protected void onPreExecute() {
    super.onPreExecute();
    pd = new ProgressDialog(c);
    pd.setTitle("Downloading..");
    pd.setMessage("Please wait.....");
    pd.show();
    }
 @Override
 protected String doInBackground(Void... voids) {

    return downloadData();
    }

@Override
protected void onPostExecute(String jsonData) {
    super.onPostExecute(jsonData);
    pd.dismiss();
    if (jsonData==null){

    Toast.makeText(c,"Failed to Download",Toast.LENGTH_SHORT).show();
    }else {
    ParseMovies p= new ParseMovies(c,jsonData,lv);
    p.execute();
    }
    }
private String downloadData()
    {

    HttpURLConnection con = Connector.connect(url);
    if (con == null) {
    return null;
    }
    try{
    InputStream is = new BufferedInputStream(con.getInputStream());
    BufferedReader br= new BufferedReader(new InputStreamReader(is));
    String line;
    StringBuffer jsonData= new StringBuffer();
    while ((line=br.readLine())!=null)
    {
    jsonData.append(line+"n");
    }
    br.close();
    is.close();
    return jsonData.toString();
    } catch (IOException e) {
    e.printStackTrace();
    }

    return null;
    }
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Manoj 1997
  • 23
  • 4

1 Answers1

0

You get this error because (unlike an Activity) a Fragment is not a Context. But in your Fragment, you can use getContext() to get the context that you need. So instead of

DownloaderMovies d= new DownloaderMovies(Trending.this,url,lv);

you must use

DownloaderMovies d= new DownloaderMovies(getContext(),url,lv);

And also change the constructor of DownloaderMovies to

public DownloaderMovies(Context c, String url, ListView lv) {
    // ...
}
Headcracker
  • 522
  • 4
  • 19