【问题标题】:undefined method or constructor error while extend to BaseFragment扩展到 BaseFragment 时未定义的方法或构造函数错误
【发布时间】:2014-07-10 05:43:34
【问题描述】:
  • 我在扩展到 BaseFragment.如果我将我的 VideoDetailFragment.java 扩展到 Activity 我没有得到任何 错误。

  • 但我需要将其扩展为BaseFragment。因为我需要获取 ListView当点击那个我可以得到视频显示在那个 网址。

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

  1. 构造函数 ArrayAdapter(VideoDetailFragment, int, String[]) 未定义
  2. VideoDetailFragment 类型的方法 finish() 未定义
  3. 构造函数 Intent(VideoDetailFragment, 类)未定义
  4. 构造函数 ProgressDialog(VideoDetailFragment) 未定义

下面我在行尾提到了这些所有错误。

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


}

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

【问题讨论】:

    标签: android android-fragments fragment android-fragmentactivity


    【解决方案1】:

    @斯蒂芬。 你有没有尝试寻找你遇到的崩溃。

    请参阅以下参考资料,我认为您的错误将得到解决

    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】:

      您需要提供对Context 的引用,而不是片段。而不是adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, videoTitles),您应该使用adapter = new ArrayAdapter&lt;String&gt;(getActivity(), android.R.layout.simple_list_item_1, videoTitles);,并且类似地传递getActivity() 而不是this 用于您有错误的所有其他方法。

      【讨论】:

      • 你的回答对我也有帮助。
      【解决方案3】:

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

      而不是在这一行中传递 this ==> adapter = new 阵列适配器(这个, android.R.layout.simple_list_item_1,数据);

      像这样传递 getActivity():

      适配器 = 新的 ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1,数据);

      【讨论】:

      • 不,如果我扩展活动,我没有得到列表视图的窗口
      猜你喜欢
      • 1970-01-01
      • 2019-09-10
      • 2021-04-26
      • 2020-09-15
      • 2019-02-16
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多