2

I am facing strange issue during call of my activity with Fragment usage. I am getting error like,

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

On line 146. My Fragment code which have error is line like below

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

And My full code for same is below,

 @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) {

    }
}

Let me know if someone can help me for get out of it. Thanks.

5
  • setUserVisibleHint is in Activity or in Fragment ? and where adding mPageFlag to Activity Commented Jan 20, 2017 at 5:42
  • @ρяσѕρєяK is in Fragment and mPageFlag I am adding it on create like public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mPageFlag = getArguments().getString(ARG_PAGE_FLAG); } } Commented Jan 20, 2017 at 5:47
  • Your Context mContext is null. Commented Jan 20, 2017 at 5:53
  • @KNeerajLal what should I use then as per You ? Commented Jan 20, 2017 at 5:58
  • possible duplicate of : stackoverflow.com/questions/10919240/… Commented Jan 20, 2017 at 8:03

1 Answer 1

7

Try using isAdded():

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

So your code should be like this :

if(isAdded() && mPageFlag.equalsIgnoreCase(getString(R.string.winners))){
Sign up to request clarification or add additional context in comments.

1 Comment

This is the currect Answer. Thanks @shadygoneinsane

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.