开发者

Android Null Pointer Exception Madness?

开发者 https://www.devze.com 2023-03-29 12:26 出处:网络
So I have 3 Activities that need to all have access to the same custom class object (let\'s call it \'a\') created by me when the very first Activity is created.

So I have 3 Activities that need to all have access to the same custom class object (let's call it 'a') created by me when the very first Activity is created.

Because 'a' is made up of a whole bunch of non-primitives, I found it difficult to make Serializable or Parcelable. Instead I created a Service that binds to each Activity as they take the forefront, and gives the Activity 'a' in OnBind().

Now 'a' has a getter that gets another object of a different custom class stored with the same object (let's call it 'b'). In all three Activities, I also need to be able to access 'b'.

If I start the first Activity, use a button to navigate to the second Activity, press the "home" button on my phone, then go back into the application by pressing its icon, then navigate to the third activity by pressing a button, I get a null pointer exception on 'b' but not 'a'.

However if I add a line that prints 'a's getter for 'b' with sysout 开发者_如何转开发in the step where I press a button to get from the second Activity to the third, then there is no null pointer exception.

Does anybody know why this is and how I can resolve the issue?


Seems you want this object a to always be there!

I suggest extending application

public class YourApplication extends Application {
    YourObject A;
    @Override
    public void onCreate(){
        A = new YourObject();
    }
    //add getters or setters //
}

Now in your Activities:

YourApplication app;
app = app = ((YourApplication)getApplicationContext());

Finally in your Mainfest: Add to <application>

android:name=".YourApplication"


Sounds like you may want to consider a singleton class. An example (and description) in Java is here, but it boils down to:

public class ClassicSingleton {
    private static ClassicSingleton instance = null;
    protected ClassicSingleton() {
        // Exists only to defeat instantiation.
    }
    public static ClassicSingleton getInstance() {
        if(instance == null) {
            instance = new ClassicSingleton();
        }
        return instance;
    }
}

Now, when you want to access the singleton object, just use:

ClassicSingleton instance = ClassicSingleton.getInstance();

Sherif's solution would also work (and I was going to suggest this first, myself), but I prefer the singleton route as it's a little cleaner from an OO perspective.

Singletons have (at least) the following benefits over a global object:

  • Singletons use lazy instantiation so they are only created when they are first accessed, not when defined. If your object is costly to instantiate or maintain, it may be better to use a singleton rather than having it around for the life of the app
  • You can guarantee there will always be only one instance of a singleton - a global object can have as many instances as you have resources for
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号