java - Dagger 2错误: cannot be provided without an @inject constructor or from an @provides-annotated method

标签 java android dagger dagger-2

我对 dagger2 完全陌生,希望能帮助我:)

我正在实现一个小型演示,但出现以下错误: 错误:(20, 10) 错误:如果没有 @Inject 构建器或 @ 或 @ Produces Provides-- 带注释的方法,则无法提供 android.net.ConnectivityManager。 com.edwin.dagger1.MainActivity.connectivityManager [注入(inject)字段类型:android.net.ConnectivityManagerconnectivityManager]

public class Dagger2Application extends Application {

    private SystemComponent systemComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        systemComponent = DaggerSystemComponent.builder()
                .systemModule(new SystemModule(this))
                .build();
    }

    public SystemComponent getSystemComponent() {
        return systemComponent;
    }
}
@Module
public class SystemModule {
    private final Application application;

    public SystemModule(Application application) {
        this.application = application;
    }

    @Provides
    @Singleton
    public Context provideContext(){
        return application;
 
@Singleton
@Component(modules = SystemModule.class)
public interface SystemComponent {
    void inject(MainActivity activity);
}

public class MainActivity extends AppCompatActivity {

    @Inject
    ConnectivityManager connectivityManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Dagger2Application dagger2Application=(Dagger2Application)getApplication();
        dagger2Application.getSystemComponent().inject(this);


        boolean activeNetworkMetered = connectivityManager.isActiveNetworkMetered();
        Log.d("Network is metered? " , activeNetworkMetered+"");
    }
}

最佳答案

问题在于注入(inject) ConnectivityManager 类。我修改了一些代码。请看一下,希望对您有帮助

public interface IConnectivityManager {
    boolean getActiveNetworkMetered();
}


public class IConnectivityManagerImpl implements IConnectivityManager {

    private Context context;

    public IConnectivityManagerImpl(Context context) {
        this.context = context;
    }

    @Override
    public boolean getActiveNetworkMetered() {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return connectivityManager.isActiveNetworkMetered();
    }
}

MainActivity.class

 @Inject
    IConnectivityManager connectivityManager;
 boolean activeNetworkMetered = connectivityManagers.getActiveNetworkMetered();
        Log.d("Network is metered? " , activeNetworkMetered+"");

系统模块.class

   @Provides
    @Singleton
    public IConnectivityManager provideConnectivity(Context context){
        return new IConnectivityManagerImpl(context);
    }

关于java - Dagger 2错误: cannot be provided without an @inject constructor or from an @provides-annotated method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37783969/

相关文章:

java - 如何在 ExpandableListView 中的组上设置 LongClick [Android]

java - 将 Mysql 转换为 Sqlite 数据库

java - 我在java代码中实现了在kotlin中定义的接口(interface)。此处不允许使用 'void' 类型

java - 如何使用 Maven - intellij IDEA 在纯 java 项目中尝试 Dagger 2

java - 错误 : [Dagger/MissingBinding] [dagger. android.AndroidInjector.inject(T)] Dagger

android - 用dagger注入(inject)的值为null

java - 相同配置文件 SonarQube 忽略新实例中的一些规则

java - LibGDX applyLinearImpulse 试图实现重力....困惑

适用于 H264 流的 Android 硬件加速视频解码器

java - CSV 文件生成