This question has already been answered:

Good I do the service of getting time, which gives it through the interface. The skeleton code is:

public class TimeSyncService extends Service { private static final String TAG = "TimeSyncService"; private static final String NTP_SERVER_IP = "192.168.128.5"; private static final getNtpListenet ntpListener = null; public interface getNtpListenet { void onTimeReceived(String fileName); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate: "); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy: "); } @Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; } class JobResult { boolean has_error = false; boolean synced = false; } static class getNtpTime extends AsyncTask<Void, Void, JobResult> { private getNtpListenet listener; @Override protected JobResult doInBackground(Void... voids) { JobResult jobResult = new JobResult(); //ошибка return jobResult; } @Override protected void onPostExecute(JobResult jobResult) { super.onPostExecute(jobResult); listener.onTimeReceived("time here"); } } 

}

In the line JobResult jobResult = new JobResult (); I get the error "Can not be references from a static context". The meaning of the error is clear, but how to correct it correctly in this case?

Reported as a duplicate by participants of Eugene Krivenja , Community Spirit Mar 16 '18 at 10:41 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    Since the inner class contains a reference to an instance of the outer class, an instance of the inner class can only be created in non-static methods (or blocks) of the outer class.

    Taken from here.

    In your case, the decision may be a static JobResult .
    Better yet, just as an independent class.