扩展到BaseFragment时未定义的方法或构造函数错误

时间:2014-07-10 05:43:35

标签: android android-fragments fragment android-fragmentactivity

  • 我在扩展到时遇到了一些编译时错误 BaseFragment。如果我将VideoDetailFragment.java延长至Activity我无法获得任何 错误。

  • 但我需要将它扩展到BaseFragment。因为我需要得到它 单击ListView,我可以在其中显示视频 网址。

我的issue是我收到以下错误:

  1. 构造函数ArrayAdapter(VideoDetailFragment,int,String [])未定义
  2. 对于VideoDetailFragment类型
  3. ,方法finish()未定义
  4. 构造函数Intent(VideoDetailFragment, 类)未定义
  5. 构造函数ProgressDialog(VideoDetailFragment)未定义
  6. 下面我在线路末尾提到了这些错误。

    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();
            }
        }
    
    
    }
    

    我不知道如何解决这些问题。任何人都可以帮助我。谢谢。

3 个答案:

答案 0 :(得分:1)

您需要提供对Context的引用,而不是片段。您应该adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, videoTitles)而不是adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, videoTitles);,而不是getActivity()而不是this,而不是{{1}},而不是{{1}}。

答案 1 :(得分:1)

@stephen。 你是否试图寻找你所遇到的坠机事件。

Plz看到以下参考文献,我认为你的错误将得到解决

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?

如果您发现更多问题或需要任何帮助,请告诉我。

由于

答案 2 :(得分:1)

片段不是上下文对象。您需要使用getActivity()作为数组适配器中的第一个参数来传递Activity对象。

  

而不是在此行中传递 ==&gt; adapter = new   ArrayAdapter(这一点,                       android.R.layout.simple_list_item_1,data);

像这样传递getActivity():

  

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