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:
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;
}
}