getResources在一个常规类中,IntentService和Fragment将它们称为函数

时间:2016-08-19 07:58:50

标签: android android-resources

我有一个常规类(SOAP.java),我添加了一个函数来调用webservice,我在res / values / strings.xml中有URL和其他信息。

My Intent Service(每2分钟调用一次)和我的App片段正在使用SOAP.java中的函数,但我无法访问字符串,错误:

  

08-19 03:47:19.730 16543-17323 / fr.solutis.solutis E / AndroidRuntime:   致命异常:IntentService [EnvoieService]       处理:fr.solutis.solutis,PID:16543       显示java.lang.NullPointerException               at fr.solutis.solutis.SOAP。(SOAP.java:22)               at fr.solutis.solutis.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.java:

public class SOAP {

    private Context context;

    public SOAP(Context current){
        this.context = current;
    }

    String NAMESPACE = context.getResources().getString(R.string.NAMESPACE);
    String URL = context.getResources().getString(R.string.URL);
    String SOAP_ACTION = context.getResources().getString(R.string.SOAP_ACTION);

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


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

IntenService:

public class EnvoieService extends IntentService {


    DatabaseHandler db = new DatabaseHandler(this);

    public EnvoieService() {
        super("EnvoieService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        List<Demande> demandes = db.getAllDemandesRenvoie();
        String TAG = EnvoieService.class.getSimpleName();

        if (!(demandes.isEmpty())) {
            for (Demande cn : demandes) {
                ...                
                SOAP soap = new SOAP(this);

                Reponse ret = soap.envoieDemande("SendLead", xml);
            }
        } else {
            cancelAlarm();
        }
    }

    public void cancelAlarm() {
        Intent intent = new Intent(getApplicationContext(), EnvoieReceiver.class);
        final PendingIntent pIntent = PendingIntent.getBroadcast(this, EnvoieReceiver.REQUEST_CODE,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(pIntent);
    }
}

片段:

public class DemandeGratuite extends android.support.v4.app.Fragment {
    ...
    {
        {
            {
                {
                    {
                        {
                            {
                                {
                                AsyncSoapCall task = new AsyncSoapCall();
                                task.execute();

                                getChildFragmentManager().popBackStack();
                                mListener.onInteraction(6);

                            } catch (FileNotFoundException e) {
                                System.err.println("FileNotFoundException: " + e.getMessage());
                            } catch (IOException e) {
                                System.err.println("Caught IOException: " + e.getMessage());
                            }
                        }
                    }
                }
        );
    }
    private class AsyncSoapCall extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            SOAP soap = new SOAP(getContext());

            Reponse ret = soap.envoieDemande("SendLead", xml);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.i(TAG, "onPostExecute");
        }

        @Override
        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute");
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            Log.i(TAG, "onProgressUpdate");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

无法从服务上下文中接收资源。您必须使用活动的上下文,或者您可以尝试使用此类

public class App extends Application {
    private static Context mContext;
    public static Resources getResources() {
        return mContext.getResources();
    }
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
    }
}

然后在任意位置使用函数App.getResources().getString(your_string_id)