这是我的片段类
public class Frangment_electronics extends Fragment {
private static final String TAG = "RecyclerViewExample";
private RecyclerView mRecyclerView;
private RecyclerAdapter adapter;
private List<Listdetails> listdetails=new ArrayList<Listdetails>();
private static final String Url = "http://onam.leah.in/new/item_details.php";
private ProgressDialog progressDialog;
public Frangment_electronics(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_electronics, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(linearLayoutManager);
adapter = new RecyclerAdapter(getActivity(), listdetails);
mRecyclerView.setAdapter(adapter);
RequestQueue queue = Volley.newRequestQueue(getActivity());
showPD();
JsonArrayRequest movieReq1 = new JsonArrayRequest(Url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePD();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Listdetails item = new Listdetails();
item.setPhone_name(obj.getString("Phone"));
item.setPrice(obj.getString("Price"));
item.setThumbnail(obj.getString("Image"));
listdetails.add(item);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePD();
}
});
queue.add(movieReq1);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
private void showPD() {
if(progressDialog == null) {
progressDialog = new ProgressDialog(getActivity());
progressDialog .setMessage("Loading...");
progressDialog .setCancelable(false);
progressDialog .setCanceledOnTouchOutside(false);
progressDialog .show();
}
}
private void hidePD() {
if (progressDialog != null) {
progressDialog .dismiss();
progressDialog = null;
}
}
@Override
public void onDestroy() {
super.onDestroy();
hidePD();
}
}
我收到以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference at
info.androidhive.Groupdeal.app.MySingleton.getRequestQueue(MySingleton.java:61) at info.androidhive.Groupdeal.app.MySingleton.<init>(MySingleton.java:26)
答案 0 :(得分:0)
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
RequestQueue queue = Volley.newRequestQueue(getActivity());
将getActivity()替换为上面几行中的rootView.getContext()。您还有许多其他地方使用过getActivity()。用rootView.getContext()替换它们。在片段中,您不能直接调用活动,而是必须使用与布局inflater创建的视图关联的任何上下文。
编辑:将showPD()代码更改为此
private void showPD(Context context) {
if(progressDialog == null) {
progressDialog = new ProgressDialog(context);
progressDialog .setMessage("Loading...");
progressDialog .setCancelable(false);
progressDialog .setCanceledOnTouchOutside(false);
progressDialog .show();
}
}
并像这样称呼它
Context context = rootView.getContext;
showPD(context);