我试图访问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>
我在尝试拨打电话:package fr.package;
公共类SOAP扩展了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) {
}
}
错误:
08-18 04:21:18.839 27859-27874 /? E / AndroidRuntime:致命异常: IntentService [EnvoieService] 处理:fr.package,PID:27859 显示java.lang.NullPointerException 在android.content.ContextWrapper.getResources(ContextWrapper.java:89) 在fr.package.SOAP。(SOAP.java:37) at fr.package.notifications.EnvoieService.onHandleIntent(EnvoieService.java:96) 在android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:65) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:136) 在android.os.HandlerThread.run(HandlerThread.java:61)
我如何调用SOAP类:
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;
}
编辑:在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);
}
}
它不起作用
答案 0 :(得分:0)
您无法从资源初始化字段;该字段需要在初始化类时初始化,并且在应用程序资源在运行时绑定之前发生。 (顺便说一句,你不能使用Resources.getSystem()的原因是你获得的Resources对象只包含系统资源,而不包含任何应用程序资源。)
请参阅此回答How to use getString() on static String before onCreate()?
获取AppController实例:
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;
}
在AppController类中添加此字段和函数(扩展Application Class的AppController类。在您的情况下将是soap)
下次使用getInstance()方法获取Soap实例
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;
}
答案 1 :(得分:0)
使用下面的任何一个
start /wait cmd /c "echo.|some.exe"
或
context.getString(R.string.resource_name)