gpt4 book ai didi

java.lang.IllegalStateException : Fragment not attached to Activity issue in android

转载 作者:行者123 更新时间:2023-12-03 01:40:42 24 4
gpt4 key购买 nike

在使用 fragment 调用我的 Activity 期间,我遇到了奇怪的问题。我收到类似错误,

java.lang.IllegalStateException: Fragment ScoreFragment{ee2b833 id=0x7f0e0198} not attached to Activity

在第 146 行。我的有错误的 fragment 代码如下所示

if(mPageFlag.equalsIgnoreCase(getString(R.string.winners))){

我的完整代码如下,

 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mPageFlag = getArguments().getString(ARG_PAGE_FLAG);
}
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser && !mIsPageLoaded){
mContext = getActivity();
mIsPageLoaded = true;
if(mPageFlag.equalsIgnoreCase(getString(R.string.winners))){
new getcontestscorewinners(mContext).execute();
}else{ //

new getcontestscorewinnersNew(mContext).execute();
}
}
}


public class getcontestscorewinners extends AsyncTask<String, Void, String> {
boolean response = false;

private Context mContext;

public getcontestscorewinners(Context context) {
mContext = context;
}

@Override
protected void onPreExecute() {

progress = ProgressDialog.show(mContext, "Processing...",
"Please wait....");
}

@Override
protected String doInBackground(String... params) {

NetworkTask.getContestScoreWinners(winnerHandler);


return "";

}

@Override
protected void onPostExecute(String result) {


}

@Override
protected void onProgressUpdate(Void... values) {

}
}

请告诉我是否有人可以帮助我摆脱困境。谢谢。

最佳答案

尝试使用isAdded():

Return true if the fragment is currently added to its activity.

所以你的代码应该是这样的:

if(isAdded() && mPageFlag.equalsIgnoreCase(getString(R.string.winners))){

关于java.lang.IllegalStateException : Fragment not attached to Activity issue in android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41756729/

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