Android 嗨,我试图在我的片段中实现回收器视图,但不幸的是应用程序崩溃了
这是我的片段类Android 嗨,我试图在我的片段中实现回收器视图,但不幸的是应用程序崩溃了,android,android-recyclerview,Android,Android Recyclerview,这是我的片段类 public class Frangment_electronics extends Fragment { private static final String TAG = "RecyclerViewExample"; private RecyclerView mRecyclerView; private RecyclerAdapter adapter; private List<Listdetails> listdetails=ne
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)
将上面几行中的getActivity替换为rootView.getContext。还有许多其他地方使用过getActivity。将它们全部替换为rootView.getContext。在片段中,您不能直接调用活动,而是必须使用与布局充气器创建的视图相关联的任何上下文
编辑:将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);
你能发布堆栈跟踪/logcat以便我能准确地看到问题所在吗?java.lang.NullPointerException:尝试在info.androidhive.Groupdeal.app.MySingleton.getRequestQueueMySingleton.java:61 at的空对象引用上调用虚拟方法“android.content.Context.getApplicationContext”info.androidhive.Groupdeal.app.MySingleton.MySingleton.java:26抱歉,Varun我不能在这里发布我的所有代码,因为它显示了一些格式化错误,所以我只能在这里发布我的片段。在我的项目中,我为recyclerview编写了listviewholder和适配器类。请阅读以下内容,我将所有getActivity替换为rootView.getContext,除了下面的方法私有void showPD{ifprogressDialog==null{progressDialog=new ProgressDialoggetActivity;progressDialog.setMessageLoading…;progressDialog.setCancelablefalse;progressDialog.setCanceledOnTouchOutsidefalse;您也可以更改它。如果它崩溃,请告诉我,并发布日志。我无法在私有void showPD方法上更改它添加了一个edit、 这将解决您的问题。请记住也为hidePD执行此操作。应用程序再次崩溃,我的logcat为java.lang.NullPointerException:尝试在info.androidhive.Groupdeal.app.MySingleton.getRequestQu上的空对象引用上调用虚拟方法“android.content.Context.getApplicationContext”eueMySingleton.java:61
Context context = rootView.getContext;
showPD(context);