0
  • I am getting some compile time error while extend to BaseFragment.If I extend my VideoDetailFragment.java to Activity I doesn't get any errors.

  • But I need to extend it to BaseFragment.Because I need to get the ListViewwhile clicking that I can get the video displayed in that url.

My issue is I am getting the Following Error:

  1. The constructor ArrayAdapter(VideoDetailFragment, int, String[]) is undefined
  2. The method finish() is undefined for the type VideoDetailFragment
  3. The constructor Intent(VideoDetailFragment, Class) is undefined
  4. The constructor ProgressDialog(VideoDetailFragment) is undefined

Below I mentioned these all errors at the end of the Line.

VideoDetailFragment.java:

package com.sit.fth.frgment;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.ProgressDialog;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.fth.android.R;
import com.sit.fth.app.BaseFragment;
import com.sit.fth.model.JSONParser;
import com.sit.fth.activity.YoutubePlayActivity;

import java.util.ArrayList;
import java.util.List;


public class VideoDetailFragment extends BaseFragment {
    // Categories must be pre-set
    private String[] data = {"Category1", "Category2", "Category3"};
    private final String TAG_VIDEOS = "videos";
    private final String TAG_CAT = "video_category";
    private final String TAG_URL = "video_url";
    private final String TAG_TITLE = "video_title";



    private List<String> videoTitles = new ArrayList<String>();
    private List<String> videoURLs = new ArrayList<String>();
    private ArrayAdapter<String> adapter;
    private Object extras;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.list1, null);




        ListView listView = ((ListView)view.findViewById(R.id.listview));


        AdapterView.OnItemClickListener clickListener = null;

        // Category view:
        if (extras == null) {
            clickListener = new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent intent = new Intent(VideoDetailFragment.this,VideoDetailFragment.class);  ----->3rd Error
                    intent.putExtra("categoryName", data[position]);
                    startActivity(intent);
                }
            };


            adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, data);  --->1st Error at this line
        }
        else { // Child view
            // Get the category of this child
            String categoryName = ((Bundle) extras).getString("categoryName");
            if (categoryName == null)

                finish();       ------>2nd Error

            // Populate list with videos of "categoryName", by looping JSON data
            new JSONParse(categoryName).execute();

            clickListener = new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent intent = new Intent(VideoDetailFragment.this, YoutubePlayActivity.class);   ----->Third Error
                    // Send video url and title to YoutubeActivity
                    intent.putExtra("videoUrl", videoURLs.get(position));
                    intent.putExtra("videoTitle", videoTitles.get(position));
                    startActivity(intent);
                }
            };
            adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, videoTitles);  ---->First Error
        }

        listView.setAdapter(adapter);
        listView.setTextFilterEnabled(true);
        listView.setOnItemClickListener(clickListener);
        return listView;
    }





    private class JSONParse extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;
        private String categoryName;

        // Constructor // Get the categoryName of which videos will be found
        public JSONParse(String category) {
            this.categoryName = category;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a loading dialog when getting the videos
            pDialog = new ProgressDialog(VideoDetailFragment.this);  ---->4th Error 
            pDialog.setMessage("Getting Videos...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        @Override
        protected JSONObject doInBackground(String... args) {
            JSONParser jParser = new JSONParser();
            // Get JSON from URL
            JSONObject json = jParser.getJSONFromUrl(JSONUrl);
            if (json == null)
                return null;

            try {
                // Get video array
                JSONArray videos = json.getJSONArray(TAG_VIDEOS);

                // Loop all videos
                for (int i=0; i<videos.length(); i++) {
                    JSONObject video = videos.getJSONObject(i);
                    Log.e("JSON:", "cat: "+video.getString(TAG_CAT)+",title: "+video.getString(TAG_TITLE)+", url: "+video.getString(TAG_URL));
                    // Check if video belongs to "categoryName"
                    if (video.getString(TAG_CAT).equals(categoryName)) {
                        addVideo(video);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return json;
        }

        private void addVideo(JSONObject video) {
            try {
                // Add title and URL to their respective arrays
                videoTitles.add(video.getString(TAG_TITLE));
                videoURLs.add(video.getString(TAG_URL));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected void onPostExecute(JSONObject json) {
            // Close the "loading" dialog
            pDialog.dismiss();
            if (json == null) {
                // Do something when there's no internet connection
                // Or there are no videos to be displayed
            }
            else // Let the adapter notify ListView that it has new items
                adapter.notifyDataSetChanged();
        }
    }


}

I doesn't know how to solve these.Anybody can help me with these.thank you.

Stephen
  • 9,899
  • 16
  • 90
  • 137

3 Answers3

1

You need to provide a reference to a Context, not a Fragment. Instead of adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, videoTitles), you should be doing adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, videoTitles);, and similarly passing in getActivity() instead of this for all the other methods where you have erros.

JustSoAmazing
  • 747
  • 4
  • 8
1

@stephen. Did u try to look for the crash where u are getting.

Plz see the following references where i think ur error will get resolved

1) Error: The constructor MainActivity.ScreenSlidePagerAdapter(FragmentManager) is undefined

2) FragmentPagerAdapter troubles and woes: Constructor Undefined

3)Why am I getting a Constructor Undefined error?

Let me know if you find some more issue or any kind of help is needed.

Thanks

Community
  • 1
  • 1
Tushar
  • 5,907
  • 15
  • 49
  • 81
1

Fragments are not context objects. You need to pass the Activity object by using getActivity() as first argument in array adapter.

Instead of passing this in this line ==> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);

pass getActivity() like this:

adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, data);

Stephen
  • 9,899
  • 16
  • 90
  • 137
Nauman Afzaal
  • 1,046
  • 12
  • 20