2016-08-19 3 views
0

私はwebserviceを呼び出す関数を追加した通常のクラス(SOAP.java)を持っていますが、res/values/strings.xmlにURLやその他の情報があります。 。IntentServiceとFragmentがそれらの関数を呼び出す通常のクラスのgetResources

私のテント(2分ごとに呼び出され)サービスと私のアプリの断片はSOAP.javaで関数を使用しているが、私は、文字列へのアクセスを得ることができない、エラー:

08-19 03:47:19.730 16543-17323/fr.solutis.solutis E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[EnvoieService] Process: 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) 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)

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

答えて

0

リソースはサービスのコンテキストからアクセスできません。あなたは、アクティビティのコンテキストを使用する必要がありますまたはuは

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)を使用することができます
関連する問題