GCX-HCI / tray

a SharedPreferences replacement for Android with multiprocess support
Apache License 2.0
2.29k stars 273 forks source link

Can't initialise new AppPreferences(getContext()) #90

Closed dfloureiro closed 7 years ago

dfloureiro commented 7 years ago

Hi everyone, here asking for some help!

It crashed in the TrayContract.java

I get a nullpointerexpection, in this method @NonNull private static String getAuthority(@NonNull final Context context) { return TextUtils.isEmpty(sTestAuthority) ? context.getString(R.string.tray__authority) : sTestAuthority; }

I believe it can't get the context.getString(R.string.tray__authority) in here.

I don't understand the issue, can somebody help?

StefMa commented 7 years ago

When you get a NullPointer that it seems that your context you put in is null. Have you checked if that is really non null?

On Mar 10, 2017 7:26 PM, "Diogo Loureiro" notifications@github.com wrote:

Hi everyone, here asking for some help!

It crashed in the TrayContract.java

I get a nullpointerexpection, in this method @NonNull https://github.com/NonNull private static String getAuthority(@NonNull https://github.com/NonNull final Context context) { return TextUtils.isEmpty(sTestAuthority) ? context.getString(R.string.tray__authority) : sTestAuthority; }

I believe it can't get the context.getString(R.string.tray__authority) in here.

  • I set the the tray__authority and I cleaned the project.
  • I set one different value for every flavour, based on the the ApplicationId.
  • if I do context.getString(R.string.tray__authority) I get the right value.
  • It's the same context has the one I gave to your AppPreferences.

I don't understand the issue, can somebody help?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/grandcentrix/tray/issues/90, or mute the thread https://github.com/notifications/unsubscribe-auth/AJwYe0srS4b9-BfNcJRn4-pPGQjZekLtks5rkZW-gaJpZM4MZtYa .

dfloureiro commented 7 years ago

I'ts not null :(

StefMa commented 7 years ago

Can you post some code how do you use initialize tray and how your build.gradle looks...

On Mar 10, 2017 9:19 PM, "Diogo Loureiro" notifications@github.com wrote:

I'ts not null :(

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/grandcentrix/tray/issues/90#issuecomment-285774270, or mute the thread https://github.com/notifications/unsubscribe-auth/AJwYe-xMEI2mgQk-Ht6iS6Jcu2oDsPLqks5rkbBbgaJpZM4MZtYa .

dfloureiro commented 7 years ago

I detected the problem!

ContentProviderStorage.java public ContentProviderStorage(@NonNull final Context context, @NonNull final String module, @NonNull final Type type) { super(module, type); mContext = context.getApplicationContext(); mTrayUri = new TrayUri(mContext); mProviderHelper = new TrayProviderHelper(mContext); }

mContext is null! why aren't you using the context I gave you instead? My ApplicationContext() at this point is, in fact, null!

jannisveerkamp commented 7 years ago

Maybe we should check if the Application Context is available and otherwise use the context itself.

This can happen in Tests.

StefMa commented 7 years ago

I can't be happen that the app context is null but an activity context (for example) is not null.

Anyway. On which platform are you seeing this issue? As far as I can remember that I saw that in specific platforms getApplicationContext can return null... But don't remember exactly...

On Mar 13, 2017 4:08 PM, "jannisveerkamp" notifications@github.com wrote:

Maybe we should check if the Application Context is available and otherwise use the context itself.

This can happen in Tests.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/grandcentrix/tray/issues/90#issuecomment-286135904, or mute the thread https://github.com/notifications/unsubscribe-auth/AJwYe5g3kOTqGp_OOnotsTALmuNFhV5rks5rlVvigaJpZM4MZtYa .

dfloureiro commented 7 years ago

The context I used is the getBaseContext() from a class that extends android.app.Application Does this answer your question?

dfloureiro commented 7 years ago

basically you should not assume you can call getApplicationContext() from any context. If I give you a context on attachBaseContext() from an android.app.Application, the this.getApplicationContext() will return null! I think what @jannisveerkamp suggested is a nice solution for this.

StefMa commented 7 years ago

Probably yes, this answer my question 😀 I really didn't know what getBaseContext returns. But as far as I know this can be the wrong (or null) context...

So probably you have to change the context which you put in into the AppPreferences...

Why not just using this, when you init Tray in your Application?

On Mar 13, 2017 7:20 PM, "Diogo Loureiro" notifications@github.com wrote:

basically you should not assume you can call getApplicationContext() from any context. If I give you a context on attachBaseContext() from an android.app.Application, the this.getApplicationContext() will return null! I think what @jannisveerkamp https://github.com/jannisveerkamp suggested is a nice solution for this.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/grandcentrix/tray/issues/90#issuecomment-286197572, or mute the thread https://github.com/notifications/unsubscribe-auth/AJwYe6JhYSj4Ij1oqjJEky_O4ZSOSQlkks5rlYkCgaJpZM4MZtYa .

dfloureiro commented 7 years ago

I want to use tray to load some remote "app settings" that need to be loaded on the very launch (like theme and stuff like that). My idea is to have an alarmmanager that will start a service on background, to update this remote settings, save them using tray, that will load once the app is opened. As you know, without tray or a content provider, the app will not load the latest shared preferences (that was what I was using previously). The problem is, at this very start, this is the only context I have. I'm afraid that if I delay this process because of the context, some of this settings will not be applied at the very start. But I'll try to improvise a solution 😄

Anyway, I think it would be cool if you could do this improvement/fix in a next release 😄

Thank you for your help!

dfloureiro commented 7 years ago

I've manage to solve the issue on my code already by changing the given context 😃 Please consider this modification in the future. Thank you for your time!