2016-08-19 1 views
0

webservice를 호출하는 함수를 추가 한 일반 클래스 (SOAP.java)가 있습니다. res/values ​​/ strings.xml에 URL 및 기타 정보가 있습니다. .IntentService 및 Fragment가 함수라고 부르는 일반 클래스의 getResources

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)

: 오류,

내 의도 (2 분마다라고합니다) 서비스 내 앱의 조각은 SOAP.java에서 함수를 사용하는,하지만 난 문자열에 대한 액세스를 얻을 수 없다 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)을 사용할 수 있습니다
관련 문제