进度对话框工作正常,但是我使用setUserVisibleHint并在该函数中执行了我的AsyncTask但是在应用程序午餐后,我收到了这个错误:
所以我研究了这个,并在堆栈中看到了很多关于流的帖子以及像AsyncTask中的make context或构造函数这样的解决方案,但是没有人不工作。
这是我的片段:
public class maghalat extends Fragment {
private View myFragmentView;
private RecyclerView recyclerView;
private DataAdapter adapter;
private String TAG = MainActivity.class.getSimpleName();
public ProgressDialog pDialog;
List<jsonContent> listcontent=new ArrayList<>();
public int dog=1;
public String url = "http://memaraneha.com;
public int id;
private boolean isFragmentLoaded=false;
public Context context;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (getActivity() != null){
if (isVisibleToUser && !isFragmentLoaded ) {
new GetContacts(context).execute();
isFragmentLoaded = true;
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.maghalat, container, false);
context= container.getContext();
return myFragmentView;
}
public class GetContacts extends AsyncTask<Void, Void, Void> {
public GetContacts(Context context) {
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(context);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
id=jsonObj.getInt("pages");
JSONArray posts = jsonObj.getJSONArray("posts");
for (int i = 0; i < posts.length(); i++) {
JSONObject c = posts.getJSONObject(i);
jsonContent jsonContent=new jsonContent();
jsonContent.title=c.getString("title");
//img
JSONObject post_img=c.getJSONObject("thumbnail_images");
for (int j=0;j<post_img.length();j++)
{
JSONObject v=post_img.getJSONObject("mom-portfolio-two");
jsonContent.imgurl=v.getString("url");
}
jsonContent.pages=id;
jsonContent.curpage=dog;
listcontent.add(jsonContent);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity().getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity().getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pDialog.dismiss();
}
}
答案 0 :(得分:3)
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && !isFragmentLoaded ) {
new GetContacts().execute();
isFragmentLoaded = true;
}
}
你应该在这里检查 getActivity() null或不
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (getActivity() != null)
{
if (isVisibleToUser && !isFragmentLoaded ) {
new GetContacts().execute();
isFragmentLoaded = true;
}
}
else
{
// NPE. You Must pass Context
}
}
<强>其次强>
检查您的onAttach
方法。在这里传递上下文
将Context context;
声明为全局。
然后将context= container.getContext();
放入 onCreateView 部分
最后,您可以调用上下文而不是 getActivity();
答案 1 :(得分:0)
尝试在userVisibleHint();
中初始化pDialog @Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
pDialog = new ProgressDialog(getActivity());
new GetContacts().execute();
}
}
在预执行中
@Override
protected void onPreExecute() {
super.onPreExecute();
if(pDialog ==null)
{
pDialog = new ProgressDialog(getActivity());
}
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
答案 2 :(得分:0)
尝试使用Context
。可能是getActivity()返回null
。使用Context
方法
onAttach
private Context context;
...
...
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
传递context
pDialog = new ProgressDialog(context);