0

I'm trying to access to strings values in my res/stringsxml:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="fr">
    <string name="app_name">Solutis</string>
</resources>

Here I'm trying to do the call:package fr.package;

public class SOAP extends Application{

String NAMESPACE;
String URL;
String SOAP_ACTION;

@Override
public void onCreate() {
    super.onCreate();
    NAMESPACE = getResources().getString(R.string.NAMESPACE);
    URL = getResources().getString(R.string.URL);
    SOAP_ACTION = getResources().getString(R.string.SOAP_ACTION);
}

private static String TAG = SOAP.class.getSimpleName();


public Reponse envoieDemande(String method, String xml) {
}
    }

Error:

08-18 04:21:18.839 27859-27874/? E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[EnvoieService] Process: fr.package, PID: 27859 java.lang.NullPointerException at android.content.ContextWrapper.getResources(ContextWrapper.java:89) at fr.package.SOAP.(SOAP.java:37) at fr.package.notifications.EnvoieService.onHandleIntent(EnvoieService.java:96) at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.os.HandlerThread.run(HandlerThread.java:61)

How I call the SOAP class:

private class AsyncSoapCall extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        SOAP soap = new SOAP();
        //soap.envoieDemande("SendLead", xml);

        Reponse ret = soap.envoieDemande("SendLead", xml);
        System.out.println(ret.getCode() + ret.getMessage() + ret.getOption());

        if (ret.getCode().equals("1")) {
            GoogleAnalytics ga= new GoogleAnalytics(getActivity());
            ga.envoieTracker(idApplication, demandeId, logement, typeForm);
        }
        return null;
    }

Edit: Initialize in onCreate()

public class SOAP extends Application {

    String NAMESPACE;
    String URL;
    String SOAP_ACTION;

    @Override
    public void onCreate() {
        NAMESPACE = getResources().getString(R.string.NAMESPACE);
        URL = getResources().getString(R.string.URL);
        SOAP_ACTION = getResources().getString(R.string.SOAP_ACTION);
    }
}

It doesn't work

Ben
  • 15
  • 4
  • Possible duplicate of [Calling context.getResources() returns null](http://stackoverflow.com/questions/24374027/calling-context-getresources-returns-null) – X3Btel Aug 18 '16 at 08:55
  • put this line `String NAMESPACE = context.getResources().getString(R.string.app_name);` in `onCreate` **AFTER** `super.onCreate()`. – Vucko Aug 18 '16 at 11:35
  • @Vucko Strangly it doesn't go into onCreate in debug mode – Ben Aug 18 '16 at 12:11
  • That's because you have 2 `onCreate` methods, use the one with `@Override` and delete the other one. – Vucko Aug 18 '16 at 12:15
  • @Vucko Yes I just editedbut it doesn't go inside, look my edit please – Ben Aug 18 '16 at 12:25
  • Did it ever enter `onCreate`? Search how to instantiate the application in Android. – Vucko Aug 18 '16 at 12:35
  • @Vucko Or I can extends it to Activity ? – Ben Aug 18 '16 at 13:01
  • @Vucko Why it's too hard to get access to strings, I just want a class which I can put functions to use them in other classes – Ben Aug 18 '16 at 13:03
  • Why do you extend Application then? You want a regular class? Make a regular class. – Vucko Aug 18 '16 at 13:06
  • @Vucko So then how I can get access to the strings values ? – Ben Aug 18 '16 at 13:11

2 Answers2

0

You cannot initialize a field from resources; the field needs to be initialized at the time the class is initialized and that happens before the application resources have been bound at run time. (By the way, the reason you cannot use Resources.getSystem() is that the Resources object you obtain that way contains only system resources, not any application resources.)

See this answer How to use getString() on static String before onCreate()?

Getting AppController Instance:

 String NAMESPACE;
 String URL;
 String SOAP_ACTION;
 private static SOAP mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        NAMESPACE = getResources().getString(R.string.NAMESPACE);
        URL = getResources().getString(R.string.URL);
        SOAP_ACTION = getResources().getString(R.string.SOAP_ACTION);

    }

    public static synchronized SOAP getInstance() {
        return mInstance;
    }

Add this field and functions in your AppController Class (AppController Class that extends Application Class. Which will be soap in your case)

And next time get Soap instance using getInstance() method

private class AsyncSoapCall extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        SOAP soap = SOAP.getInstance();
        //soap.envoieDemande("SendLead", xml);

        Reponse ret = soap.envoieDemande("SendLead", xml);
        System.out.println(ret.getCode() + ret.getMessage() + ret.getOption());

        if (ret.getCode().equals("1")) {
            GoogleAnalytics ga= new GoogleAnalytics(getActivity());
            ga.envoieTracker(idApplication, demandeId, logement, typeForm);
        }
        return null;
    }
Community
  • 1
  • 1
Muhammad Usman
  • 200
  • 1
  • 14
  • So I can initialize variable in onCreate – Ben Aug 18 '16 at 08:46
  • yes it will work, you have to initialize it onCreate – Muhammad Usman Aug 18 '16 at 08:50
  • did you remove it from outside onCreate() – Muhammad Usman Aug 18 '16 at 09:42
  • @Ben add super.onCreate(); in onCreate() method at top before intializing – Muhammad Usman Aug 18 '16 at 09:45
  • It should work i have a similar class extending Application. And i am accessing getResources().getString(); from there and it is working there – Muhammad Usman Aug 18 '16 at 11:25
  • @Ben why you are creating soap object everytime. Classes that extends Application are automatically generated when apps run. So instead of making new object just call its instance like I edited in my solution – Muhammad Usman Aug 18 '16 at 11:27
  • Strangly it doesn't go into onCreate in debug mode, I prefer create SOAP because I'm using notifications. – Ben Aug 18 '16 at 12:14
  • @Ben it'll be called only first time automatically and at that time your strings will get initialized., you don't have to create any instance just call getInstance(). Method And you should not make instance of AppController Class – Muhammad Usman Aug 18 '16 at 13:11
  • I'm testing it, it is obligate to extends application to access strings values in a class like that ? – Ben Aug 18 '16 at 13:29
  • Plus, there could be something wrong with your asyncTask . First Show toast in on create method of AppController to check whether string are getting initialized and they will get initialized. I am currently using this strategy in my own app, right now – Muhammad Usman Aug 18 '16 at 13:39
0

Use either one from below

context.getString(R.string.resource_name)

or

application.getString(R.string.resource_name)
Soham
  • 4,397
  • 11
  • 43
  • 71
Rahul
  • 1,380
  • 1
  • 10
  • 24