数组和适配器不能调用虚空片段
Posted
技术标签:
【中文标题】数组和适配器不能调用虚空片段【英文标题】:Array and Adapter cant called on Void Fragment 【发布时间】:2016-11-09 18:47:49 【问题描述】:请帮助我。我的家庭片段是空白的。我无法将 JSON 数组打印到我的主片段。我希望 ListView 显示来自 API 的 Json 数组的数据,我如何使用片段放置来自 Json 的数组数据。对不起我的英语。
我正在使用片段:
public class HomeFragment extends Fragment implements OnFeedListener
ListView listView;
FeedAdapter adapter;
ArrayList<Post> posts;
View myView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
myView = inflater.inflate(R.layout.home, container, false);
return myView;
@Override
public void onActivityCreated(Bundle savedInstanceState)
super.onActivityCreated(savedInstanceState);
@Override
public void onFeed(JSONArray array)
posts = new ArrayList<>();
int lenght = array.length();
for(int i = 0; i < lenght; i++)
JSONObject object = array.optJSONObject(i);
Post post = new Post(object.optString("title"), object.optString("excerpt"), object.optString("thumbnail"));
posts.addAll(posts);
adapter.addAll(posts);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
FeedTask task = new FeedTask(this);
task.execute("http://indo-coc.com/api/get_recent_posts/");
public class FeedTask extends AsyncTask<String, Void, JSONArray>
private OnFeedListener listener;
public FeedTask(OnFeedListener listener)
this.listener = listener;
@Override
protected JSONArray doInBackground(String... params)
String url = params[0];
OkHttpClient client = new OkHttpClient();
Request.Builder builder = new Request.Builder();
Request request = builder.url(url).build();
try
Response response = client.newCall(request).execute();
String json = response.body().string();
try
JSONObject object = new JSONObject(json);
JSONArray array = object.optJSONArray("posts");
return array;
catch (JSONException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
return null;
@Override
protected void onPostExecute(JSONArray array)
super.onPostExecute(array);
if(null == array)
return;
if(null != listener )
listener.onFeed(array);
public class FeedAdapter extends ArrayAdapter<Post>
private int resource;
public FeedAdapter(Context context, int resource)
super(context, resource);
this.resource = resource;
@Override
public View getView(int position, View convertView, ViewGroup parent)
// Convert View -> Reuse
if (null==convertView)
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(resource, null);
// Binding Data
Post post = getItem(position);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView desc = (TextView) convertView.findViewById(R.id.description);
title.setText(post.title);
desc.setText(post.description);
return convertView;
public class Post
public String title;
public String description;
public String thumbnail; //URL
public Post(String title, String desc, String thumbnail)
this.title = title;
this.description = desc;
this.thumbnail = thumbnail;
【问题讨论】:
【参考方案1】:将FeedAdapter adapter
和ArrayList<Post> posts
声明为公共类中的全局变量。它们必须在public void onActivityCreated(Bundle savedInstanceState)
之外声明
【讨论】:
【参考方案2】:这里发生了什么:
在 onActivityCreated 中,您声明局部变量而不为它们分配值。因为它们是本地的,所以一旦方法终止,它们就会消失。将方法上方的变量声明为类变量,以便稍后在onFeed
中访问它们
您没有为变量listView
和adapter
赋值。我假设在其他地方,通常的做法是在onViewCreated
中执行此操作,您为片段设置主视图。如果您不知道如何执行此操作,请参阅 this question。然后,您可以使用findViewById
加载 listView,假设 listView 位于您在片段中膨胀的布局中。然后你必须创建一个新的FeedAdapter
并使用listView.setAdapter(adapter)
将它分配给listView
祝你好运!
【讨论】:
【参考方案3】:试试这个:
class MyFragment extends Fragment
ListView listView;
FeedAdapter adapter;
ArrayList<Post> posts;
public void onCreate(Bundle b)
super.onCreate(b);
//My bad listview will only be assigned properly in onCreateView once the view is inflated
posts = new ArrayList<>();
adapter=new FeedAdapter(getActivity(),R.id.yourresouceid,posts); // which ever way your adapter is defined
listView.setAdapter(adapter);
public void onActivityCreated(Bundle savedInstanceState)
super.onActivityCreated(savedInstanceState);
public void onFeed(JSONArray array)
int lenght = array.length();
for (int i = 0; i < lenght; i++)
JSONObject object = array.optJSONObject(i);
posts.add(post);
//adapter.addAll(posts); //you don't need to do this, already done when creating your adapter
adapter.notifyDataSetChanged();
说明:onActivityCreated 的范围只到它的右括号。在内部声明的任何内容对任何外部函数都是不可见的。有关其工作原理的更多详细信息,请参阅 java 中的范围解析。
您还必须在使用它们之前对其进行初始化。只是声明它并尝试使用它会给出 NullPointerExceptions
【讨论】:
很遗憾,抽屉停止了以上是关于数组和适配器不能调用虚空片段的主要内容,如果未能解决你的问题,请参考以下文章
如何将数据从回收器适配器发送到片段 |如何从 recyclerview 适配器调用片段函数