GeofenceTransitionsIntentService’ не имеет конструктора по умолчанию

#geofencing

#геозона

Вопрос:

Я работаю с геозонированием. Я написал класс serivice GeofenceTransitionsIntentService, и он показывает красную метку и говорит, что не имеет конструктора по умолчанию. пожалуйста, помогите мне.

Это мой файл манифеста :

 <?xml version="1.0" encoding="utf-8"?>
  

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
     <!--optional (needed if default theme has no action bar) -->
    <activity android:name=".loginpage"></activity>
    <activity android:name=".Userlogin" />
    <activity android:name=".Register" />
    <activity android:name=".MainActivity">

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyB9DNBzfrYiTcmUThheWGNdAKY3lRU3pi8" />


        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

    <service android:name=".GeofenceTransitionsIntentService"/> // here's the error. it turned up red.


</application>
  

вот мой класс Java. у него есть конструктор, но не по умолчанию, если я добавляю конструктор вручную, он показывает ошибку.

 package com.example.foodtag;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingEvent;

import java.util.ArrayList;
import java.util.List;

public class GeofenceTransitionsIntentService extends IntentService {
    GeofencingEvent geofencingEvent;
/**
 * Creates an IntentService.  Invoked by your subclass's constructor.
 *
 * @param name Used to name the worker thread, important only for debugging.
 */
public GeofenceTransitionsIntentService(String name) { //here is the constructor but it is not default constructor.
    super(name);
}

GeofenceTransitionsIntentService(){ // and if i create constructor without parameters, it is showing an error "There is no default constructor available in 'android.app.IntentService'"

}


@Override
protected void onHandleIntent( Intent intent) {  

    geofencingEvent = GeofencingEvent.fromIntent(intent);
    if(geofencingEvent.hasError()){
        Toast.makeText(this, "Event has error", Toast.LENGTH_SHORT).show();
    return;
    }

    int geoFenceTransition = geofencingEvent.getGeofenceTransition();

    if (geoFenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER){

        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
      // String geofenceTransitionDetails = getGeofenceTransitionDetails(this, geoFenceTransition,triggeringGeofences);

      //  sendNotofication();

    }else {
        Toast.makeText(this, "geofence transition invalid", Toast.LENGTH_SHORT).show();        }
}

private  String getGeofenceTransitionDetails(
        Context context,
        int geofenceTransition,
        List<Geofence> triggeringGeofences){
   // String geofenceTransitionString = getTransitionString(geofenceTransition);//getTransitionString(geoFenceTransition)

    ArrayList triggeringgeofencesIdsList = new ArrayList();
    for (Geofence geofence : triggeringGeofences){
        triggeringgeofencesIdsList.add(geofence.getRequestId());
    }

    String triggeringgeofencesIdsString = TextUtils.join(",",triggeringgeofencesIdsList);

    return  triggeringgeofencesIdsString;
}
  

}

Ответ №1:

Создайте конструктор по умолчанию (без параметров) и добавьте super с именем класса.

  public GeofenceTransitionsIntentService(){
    super("GeofenceTransitionsIntentService"); //add this to avoid the error.
}