Ben
Ben

Reputation: 15

Can"t acess to strings.xml resources in a class

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

Upvotes: 0

Views: 799

Answers (2)

Muhammad Usman
Muhammad Usman

Reputation: 200

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;
    }

Upvotes: 0

Rahul
Rahul

Reputation: 1422

Use either one from below

context.getString(R.string.resource_name)

or

application.getString(R.string.resource_name)

Upvotes: 0

Related Questions