1

I have my BaseApplication lets say which looks like

public class ApplicationBase extends Application {
    String someKey;
    public String getSomeKey() {
        return someKey;
    }

    public void setSomeKey(String someKey) {
        this.someKey = someKey;
    }
 }

I have a fragment It performs some actions and decides on the basis of

String key = (ApplicationBase) getActivity().getApplication()).getSomeKey();

if(key.equals(anotherString){
   Do Some thing
   ...
}else{
   Do Some thing
   ....
}

It run smoothly but sometime (rare case) It crashes with this error

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Application android.support.v4.app.FragmentActivity.getApplication()' on a null object reference

How to solve? (I tried my best to keep this question universal not personal so that another coder relates this question with his problem So please Dont downvote :p)

Or Can I do this to prevent this error?

 if((ApplicationBase) getActivity().getApplication() !=null){

     String key = (ApplicationBase) getActivity().getApplication()).getSomeKey();

     if(key.equals(anotherString){
         Do Some thing
         ...
     }else{
         Do Some thing
         ....
     }
 }
5
  • You can avoid the error by using your code FragementActivity.getApplication in a try catch block. Commented Mar 3, 2018 at 22:09
  • May I know why this error is occurring ? Commented Mar 3, 2018 at 22:11
  • 1
    Because when you invoke this method, sometime the context has not been initialized, and if the context has not been initialized and you use the context, you will get null point error. Commented Mar 3, 2018 at 22:13
  • 1
    getActivity() can return null in two cases . Your fragment not attached to Activity yet and your fragment get destroyed . Commented Mar 3, 2018 at 22:14
  • so should I do what I have written after bold text? Commented Mar 3, 2018 at 22:18

2 Answers 2

3

Your fragment has not attached to your activity yet or has got already destroyed. Try to get your key in onAttach() method

Sign up to request clarification or add additional context in comments.

Comments

1

As @shmakova already pointed, you can't get the activity host of the fragment before the fragment is attached to the activity. So, you need to get the activity inside the onAttach() or after the onAttach() is called. You can use a flag too, like this:

public class YourFragment extends Fragment {
  private boolean mIsAttached = false;

  ...

  protected void onAttach() {
    mIsAttached = true;
  }

  private void doSomething() {
    if(mIsAttached) {
      // I am attached. do the work!
    }
  }
}

Side note:

If you're depend on the Application class, you can use Application class directly by making the Application class as singleton (although Application is already singleton) like this:

public class YourApplication extends Application {

  private static YourApplication sInstance;
  private String someKey;

  public static YourApplication getInstance() {
    return sInstance;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    sInstance = this;
  }

  public String getSomeKey() {
    return someKey;
  }

  public void setSomeKey(String someKey) {
    this.someKey = someKey;
  }
}

then you can call the method with:

String key = YourApplication.getInstance().getSomeKey();

Comments

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.