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を見つけることができません。 同じコードがアクティビティで正常に動作していますが、アクティビティで使用しているときにエラーが発生しています。フラグメント内のシンボル変数Rを見つけることができません
'onCreateView()'ビュー内で 'getView()'を呼び出すと、明らかな理由でNPEが発生します –