在Fragment中找不到符号变量R.

时间:2017-09-23 09:56:43

标签: java android android-fragments android-arrayadapter

public class home extends Fragment{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //returning our layout file
        //change R.layout.yourlayoutfilename for each of your fragments

        postlist = (ListView)getView().findViewById(R.id.postList);
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("Loading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.show();

        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                gson = new Gson();
                list = (List) gson.fromJson(s, List.class);
                postTitle = new String[list.size()];

                for (int i = 0; i < list.size(); ++i) {
                    mapPost = (Map<String, Object>) list.get(i);
                    mapTitle = (Map<String, Object>) mapPost.get("title");
                    postTitle[i] = (String) mapTitle.get("rendered");
                }
                **postlist.setAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, postTitle));//R is not Resolved**
                progressDialog.dismiss();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyerror) {
                Toast.makeText(getActivity(),"Some error occurred", Toast.LENGTH_LONG).show();

            }
        });

        return inflater.inflate(R.layout.home, container, false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //you can set the title for your toolbar here for different fragments different titles
        getActivity().setTitle("Home");
    }
}

我收到错误:找不到符号变量R.如何解决它。 相同的代码在活动中运行良好,但在活动中使用它时会产生错误。

3 个答案:

答案 0 :(得分:1)

我发生了同样的事情。
我尝试按照建议引用rootView,但错误仍然存​​在。
我尝试了Build-&gt; clean&amp; amp;重建,但错误仍然存​​在。

我最终以删除我的片段和布局对应,然后通过android Studio界面重新创建了一个新的片段(New =&gt; Fragment =&gt; Fragment (空白)),并实现了与之前完全相同的代码,并且工作正常。

我知道,这不是您可能正在寻找的解决方案,但在花了太多时间尝试我在网上找到的所有建议之后,这是唯一对我有用的方法。

答案 1 :(得分:0)

尝试将这些控件绑定在片段

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

  View rootView = inflater.inflate(R.layout.home, container, false);

 postlist = (ListView)rootView.findViewById(R.id.postList);

  // your allll code
  return rootView;
}

答案 2 :(得分:0)

试用此代码

View rootView
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    if (rootView != null) {
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null)
            parent.removeView(rootView);
    }
    try {
        rootView = inflater.inflate(R.layout.home, container, false);
    } catch (InflateException e) {

    }



return rootView;
}