gpt4 book ai didi

java - Android Fragment 中的 Volley 请求队列(getApplicationContext 可能会产生 NullPointerException)

转载 作者:行者123 更新时间:2023-12-02 02:07:16 25 4
gpt4 key购买 nike

我只想使用 fragment 内的recyclerview从本地主机加载数据,但没有显示任何内容,并且它说getApplicationContext可能会产生NullPointerException

错误出现在

Volley.newRequestQueue(getActivity().getApplicationContext()).add(stringRequest);

我在主要 Activity 上尝试了代码,它工作正常。

public class UpdateFragment extends Fragment {

private static final String URL = "http://192.168.1.32/CAGELCOII_APP/api.php";

RecyclerView recyclerView;
ItemAdapter adapter;

List<Item> itemList;

public UpdateFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_update, container, false);

itemList = new ArrayList<>();
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

loadItem();

adapter = new ItemAdapter(getActivity(), itemList);
recyclerView.setAdapter(adapter);

return view;
}

private void loadItem(){

StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {

try {
JSONArray products = new JSONArray(response);

for(int i =0; i < products.length(); i++){
JSONObject productObject = products.getJSONObject(i);

int id = productObject.getInt("id");
String description = productObject.getString("description");
String agency = productObject.getString("agency");
String date = productObject.getString("date");
String time = productObject.getString("time");
String image = productObject.getString("image");

Item item = new Item(id, description, agency, date, time, image);
itemList.add(item);
}

adapter = new ItemAdapter(getActivity(), itemList);
recyclerView.setAdapter(adapter);


} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});

Volley.newRequestQueue(getActivity().getApplicationContext()).add(stringRequest);

}

}

最佳答案

调用 getActivity() 时,您必须确保 fragment isAdded() 已添加到 Activity,否则您将收到 NullPointerException因为 getActivity() 返回托管该 fragment 的 Activity 。

如果您始终想使用应用程序上下文,由于其非死亡生命周期,您可以使用此静态函数在整个应用程序中检索它:

App.java

public class App extends Application {
private static Context sAppContext;

public void onCreate() {
super.onCreate();
sAppContext = this;
}

public static Context getContext() {
return sAppContext;
}
}

AndroidManifest.xml

...
<application
android:name=".App"
...>
</application>

您可以在代码中使用它:

Volley.newRequestQueue(App.getContext()).add(stringRequest);

关于java - Android Fragment 中的 Volley 请求队列(getApplicationContext 可能会产生 NullPointerException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57351795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com