在工具栏的活动中,我得到了一个按钮,需要从该片段中的片段和更新列表中调用方法.现在这是一个错误.
打电话给活动
打电话给活动
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.menu_sort: ListFragment listFragment = new ListFragment(); listFragment.sortByPopularity(); break; } return super.onOptionsItemSelected(item); }
public class ListFragment extends Fragment implements ListAdapter.ItemClickListener { /** * Needed */ RecyclerView recyclerView; View view; List<BasePojo.Result> list; ListAdapter listAdapter; public ListFragment() { } @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { /** * Main Initialization */ view = inflater.inflate(R.layout.fragment_list,container,false); recyclerView = view.findViewById(R.id.recycler_list_detailed); recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2)); list = new ArrayList<>(); listAdapter = new ListAdapter(list,setOnItemClickCallback()); recyclerView.setAdapter(listAdapter); RetrofitClient.getApiService().getPhotosList(getString(R.string.api_key)).enqueue(new Callback<BasePojo>() { @Override public void onResponse(Call<BasePojo> call,Response<BasePojo> response) { BasePojo basePojo = response.body(); list.addAll(basePojo.getResults()); recyclerView.getAdapter().notifyDataSetChanged(); } @Override public void onFailure(Call<BasePojo> call,Throwable t) { Log.d("tag","Response Failed" + t.toString()); } }); return view; } @Override public void onItemClick(View view,int position) { Log.v("in on click","value " + position); } private OnItemClickListener.OnItemClickCallback setOnItemClickCallback() { OnItemClickListener.OnItemClickCallback onItemClickCallback = new OnItemClickListener.OnItemClickCallback() { @Override public void onItemClicked(View view,int position) { BasePojo.Result itemClicked = list.get(position); Bundle bundle = new Bundle(); bundle.putString("title",itemClicked.getOriginalTitle()); bundle.putString("overview",itemClicked.getOverview()); bundle.putString("release_date",itemClicked.getReleaseDate()); bundle.putString("vote_average",itemClicked.getVoteAverage().toString()); bundle.putString("poster_path",itemClicked.getPosterPath()); DetailedFragment detailedFragment = new DetailedFragment(); detailedFragment.setArguments(bundle); FragmentManager manager = getActivity().getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.main_frame_list,detailedFragment); Log.d("tag","title is 111 " + bundle.get("title")); transaction.commit(); } }; return onItemClickCallback; } @Override public void onAttachFragment(Fragment childFragment) { super.onAttachFragment(childFragment); } public void sortByPopularity() { RetrofitClient.getApiService().getPopularList(getString(R.string.api_key)).enqueue(new Callback<BasePojo>() { @Override public void onResponse(Call<BasePojo> call,"Response Failed" + t.toString()); } }); } }
这是一个错误
05-09 12:48:26.915 5775-5775/com.borisruzanov.popularmovies E/AndroidRuntime: FATAL EXCEPTION: main Process: com.borisruzanov.popularmovies,PID: 5775 java.lang.IllegalStateException: Fragment ListFragment{6dbd6de} not attached to a context. at android.support.v4.app.Fragment.requireContext(Fragment.java:614) at android.support.v4.app.Fragment.getResources(Fragment.java:678) at android.support.v4.app.Fragment.getString(Fragment.java:700) at com.borisruzanov.popularmovies.ListFragment.sortByPopularity(ListFragment.java:110) at com.borisruzanov.popularmovies.MainActivity.onOptionsItemSelected(MainActivity.java:47) at android.app.Activity.onMenuItemSelected(Activity.java:3204) at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:407) at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195) at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108) at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108) at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:63) at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:203) at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:780) at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822) at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171) at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973) at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963) at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624) at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:150) at android.view.View.performClick(View.java:5610) at android.view.View$PerformClick.run(View.java:22265) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
非常感谢您的时间和帮助.如果我的问题看起来不太好请注意,我将教会如何更好地提问
解决方法
创建片段实例是不够的.它需要通过事务附加到Activity
getFragmentManager().beginTransaction().replace(R.id.container_layout,fragment).commit();
成功提交后,调用片段中的onAttach方法,创建vew,然后您可以与其视图进行交互.
在您的情况下,创建片段实例并将其附加到activity onCreate,然后在click事件中调用sortByPopularity
阅读有关片段生命周期的更多信息:https://developer.android.com/guide/components/fragments