gioton gioton - 5 months ago 5
Android Question

JsonParsing error in NavigationDrawer frame

I am trying to get the info from the url in my first fragment, but it seems my first fragment is giving me 2 errors

first error here

mProgressDialog = new ProgressDialog(FirstFragment.this);

and second here

adapter = new ListViewAdapter(FirstFragment.this, arraylist);

This is my FirstFragment class

public class FirstFragment extends Fragment {

View myView;


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.first_layout, container ,false);
new DownloadJSON().execute();
return myView;
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String RANK = "rank";
static String COUNTRY = "country";
static String POPULATION = "population";
static String FLAG = "flag";

// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {

protected void onPreExecute() {
// Create a progressdialog
mProgressDialog = new ProgressDialog(FirstFragment.this);
// Set progressdialog title
mProgressDialog.setTitle("Actualizando InformaciĆ³n");
// Set progressdialog message
// Show progressdialog;

protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions

try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("worldpopulation");

for (int i = 0; i < jsonarray.length(); i++) {

HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("rank", jsonobject.getString("rank"));
map.put("country", jsonobject.getString("country"));
map.put("population", jsonobject.getString("population"));
map.put("flag", jsonobject.getString("flag"));
// Set the JSON Objects into the array

} catch (JSONException e) {
Log.e("Error", e.getMessage());
return null;

protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) myView.findViewById(;
// Pass the results into
adapter = new ListViewAdapter(FirstFragment.this, arraylist);
// Set the adapter to the ListView
// Close the progressdialog

This is my listviewadapter class

public class ListViewAdapter extends BaseAdapter {

// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();

public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);

public int getCount() {
return data.size();

public Object getItem(int position) {
return null;

public long getItemId(int position) {
return 0;

public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView rank;
TextView country;
TextView population;
ImageView flag;

inflater = (LayoutInflater) context

View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);

// Locate the TextViews in listview_item.xml
rank = (TextView) itemView.findViewById(;
country = (TextView) itemView.findViewById(;
population = (TextView) itemView.findViewById(;

// Locate the ImageView in listview_item.xml
flag = (ImageView) itemView.findViewById(;

// Capture position and set results to the TextViews
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(FirstFragment.FLAG), flag);
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, SingleItemView.class);
// Pass all data rank
intent.putExtra("rank", resultp.get(FirstFragment.RANK));
// Pass all data country
intent.putExtra("country", resultp.get(FirstFragment.COUNTRY));
// Pass all data population
// Pass all data flag
intent.putExtra("flag", resultp.get(FirstFragment.FLAG));
// Start SingleItemView Class

return itemView;

and this my main activity class if its need

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) findViewById(;

FloatingActionButton fab = (FloatingActionButton) findViewById(;
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();

DrawerLayout drawer = (DrawerLayout) findViewById(;
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

NavigationView navigationView = (NavigationView) findViewById(;

public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(;
if (drawer.isDrawerOpen(GravityCompat.START)) {
} else {

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;

public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == {
return true;

return super.onOptionsItemSelected(item);

public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId(); fragmentManager = getFragmentManager();

if (id == {

fragmentManager.beginTransaction().replace(,new FirstFragment()).commit();
// Handle the camera action
} else if (id == {

fragmentManager.beginTransaction().replace(,new SecondFragment()).commit();

} else if (id == {

fragmentManager.beginTransaction().replace(,new ThirdFragment()).commit();

} else if (id == {

} else if (id == {


DrawerLayout drawer = (DrawerLayout) findViewById(;
return true;


Error:(58, 31) error: no suitable constructor found for ProgressDialog(FirstFragment)
constructor ProgressDialog.ProgressDialog(Context,int) is not applicable
(actual and formal argument lists differ in length)
constructor ProgressDialog.ProgressDialog(Context) is not applicable
(actual argument FirstFragment cannot be converted to Context by method invocation conversion)

Error:(107, 23) error: constructor ListViewAdapter in class ListViewAdapter cannot be applied to given types;
required: Context,ArrayList<HashMap<String,String>>
found: FirstFragment,ArrayList<HashMap<String,String>>
reason: actual argument FirstFragment cannot be converted to Context by method invocation conversion



Both times you're trying to pass the fragments to the constructors of those classes. Fragments are not activities so when constructors require contexts for instantiation you can't just pass the fragments. Instead, use the getActivity() method. This method returns the activity the fragment is associated with. So:

mProgressDialog = new ProgressDialog(getActivity());


adapter = new ListViewAdapter(getActivity(), arraylist);

Further reading: