我刚刚开始了一个食谱应用程序的个人项目,只是为了学习 API 的基础知识。我在 YouTube 上找到了一个教程,我心想,在我掌握基础知识之前,这将是一个很好的起点。我按照教程中显示的那样完成了所有操作,但没有收到任何错误。但是,当我启动程序时,应用程序崩溃了。我想我可能知道这个问题,因为在我的应用程序中我已经实现了一个底部导航应用程序,它将用户带到三个不同的片段。在教程中,显示食谱的所有最后部分都是在他们的主要活动中完成的,而我在片段中完成了这一部分。在代码中有一些部分需要上下文,我确实使用了 getContext() 函数,但我认为它从那里崩溃了,如果有人能像我一样向我解释如何更好地使用片段,我仍然会很棒开始android开发,我仍然需要掌握它。
教程:https://www.youtube.com/watch?v=6-891CSz6v0&list=PLVE5czp3KIIzdCllHLYrPt7zEH1BZ4qNi 主要代码从 51:39 开始,使用一些类和适配器从 API 链接数据并显示它们。
这是我在代码中复制的内容:
package com.example.rdproject;
import android.app.ProgressDialog;
(...)
/**
* A simple {@link Fragment} subclass.
* Use the {@link PopularFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class PopularFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public PopularFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment PopularFragment.
*/
// TODO: Rename and change types and number of parameters
public static PopularFragment newInstance(String param1, String param2) {
PopularFragment fragment = new PopularFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
ProgressDialog dialog;
RequestManager manager;
RandomRecipeAdapter randomRecipeAdapter;
RecyclerView recyclerView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_popular, container, false);
dialog.setTitle("Loading");
manager = new RequestManager(view.getContext());
manager.getRandomRecipes(randomRecipeResponseListener);
dialog.show();
return view;
}
private final RandomRecipiResponseListener randomRecipeResponseListener = new RandomRecipiResponseListener() {
@Override
public void didFetch(RandomRApiResponse response, String message) {
dialog.dismiss();
recyclerView = (RecyclerView) getView().findViewById(R.id.recyclerRandom);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 1));
randomRecipeAdapter = new RandomRecipeAdapter(getContext(), response.recipes);
recyclerView.setAdapter(randomRecipeAdapter);
}
@Override
public void didError(String message) {
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT);
}
};
}
我试过使用getContext()函数,还是不行。当我单击导航栏中的特定片段时,我的应用程序崩溃了。我不知道问题是由“视图”还是其他原因引起的,但我认为是因为这个。我已经使用 Retrofit 从 Spoonacular 获取配方数据,但我认为我完全按照教程中的步骤进行操作,应该没有任何问题。