Dagger 2:使用@Named 获取同一对象的多个实例时出错

Dagger 2 : error while getting a multiple instances of same object with @Named

如何获得多个相同 return 类型的实例,如游标

例如:-

Module
@CursorScope
public class CursorModule {


    @Provides
    Cursor provideSongCursor(
            @Named("Song") Musician musician) {
        return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                new String[]{
                        BaseColumns._ID,
                        MediaStore.Audio.AudioColumns.TITLE,
                        MediaStore.Audio.AudioColumns.ARTIST,
                        MediaStore.Audio.AudioColumns.ALBUM,
                        MediaStore.Audio.AudioColumns.DURATION
                }, MediaStore.Audio.AudioColumns.IS_MUSIC + "=1", null, null);
    }

    @Provides
    Cursor provideAlbumCursor(
            @Named("Album") Musician musician) {
        return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                new String[]{
                        BaseColumns._ID,
                        MediaStore.Audio.AlbumColumns.ALBUM,
                        MediaStore.Audio.AlbumColumns.ARTIST,
                        MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS,
                        MediaStore.Audio.AlbumColumns.FIRST_YEAR
                }, null, null, null);
    }

    @Provides
    Cursor provideArtistCursor(@Named("Artist") Musician musician) {
        return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
                new String[] {
                        BaseColumns._ID,
                        MediaStore.Audio.ArtistColumns.ARTIST,
                        MediaStore.Audio.ArtistColumns.NUMBER_OF_ALBUMS,
                        MediaStore.Audio.ArtistColumns.NUMBER_OF_TRACKS
                }, null, null,null);
    }

    @Provides
    Cursor provideGenreCursor(
            @Named("Genres") Musician musician) {
        return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI,
                new String[] {
                        BaseColumns._ID,
                        MediaStore.Audio.GenresColumns.NAME
                }, null, null, null);
    }

    @Provides
    Cursor providePlaylistCursor(@Named("Playlist") Musician musician) {
        return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
                new String[] {
                        BaseColumns._ID,
                        MediaStore.Audio.PlaylistsColumns.NAME
                }, null, null, null);
    }
}

中提供
@CursorScope
@Subcomponent(modules = CursorModule.class)
public interface CursorComponent {
    Cursor cursor();
}

我收到这个错误

Error:(17, 11) Gradle: error: android.database.Cursor is bound multiple times:
@Provides android.database.Cursor com.merkmod.musician.dependency.CursorModule.provideSongCursor(@Named("Song") com.merkmod.musician.application.Musician)
@Provides android.database.Cursor com.merkmod.musician.dependency.CursorModule.provideAlbumCursor(@Named("Album") com.merkmod.musician.application.Musician)
@Provides android.database.Cursor com.merkmod.musician.dependency.CursorModule.provideArtistCursor(@Named("Artist") com.merkmod.musician.application.Musician)
@Provides android.database.Cursor com.merkmod.musician.dependency.CursorModule.provideGenreCursor(@Named("Genres") com.merkmod.musician.application.Musician)
@Provides android.database.Cursor com.merkmod.musician.dependency.CursorModule.providePlaylistCursor(@Named("Playlist") com.merkmod.musician.application.Musician)

我制作了多个 Cursor 实例,并首先在提供者级别使用 @Named 进行了注释,然后它开始给我错误,不能提供 @Provides 注释,所以我转而在构造函数中使用它

就像上面的代码一样。问题是 运行 一次又一次的循环,我就像被困在完成光标的事情上,任何帮助都会得到赞赏。

当您想提供一种类型的多个变量时,您必须使用如下所示的 @Named 注释:

Module
@CursorScope
public class CursorModule {
@Provides
@Named("songCursor")
Cursor provideSongCursor(
        @Named("Song") Musician musician) {
    return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[]{
                    BaseColumns._ID,
                    MediaStore.Audio.AudioColumns.TITLE,
                    MediaStore.Audio.AudioColumns.ARTIST,
                    MediaStore.Audio.AudioColumns.ALBUM,
                    MediaStore.Audio.AudioColumns.DURATION
            }, MediaStore.Audio.AudioColumns.IS_MUSIC + "=1", null, null);
}

@Provides
@Named("albumCursor")
Cursor provideAlbumCursor(
        @Named("Album") Musician musician) {
    return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
            new String[]{
                    BaseColumns._ID,
                    MediaStore.Audio.AlbumColumns.ALBUM,
                    MediaStore.Audio.AlbumColumns.ARTIST,
                    MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS,
                    MediaStore.Audio.AlbumColumns.FIRST_YEAR
            }, null, null, null);
}
  @Provides
@Named("artistCursor")
Cursor provideArtistCursor(@Named("Artist") Musician musician) {
    return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
            new String[] {
                    BaseColumns._ID,
                    MediaStore.Audio.ArtistColumns.ARTIST,
                    MediaStore.Audio.ArtistColumns.NUMBER_OF_ALBUMS,
                    MediaStore.Audio.ArtistColumns.NUMBER_OF_TRACKS
            }, null, null,null);
}

@Provides
@Named("genreCursor")
Cursor provideGenreCursor(
        @Named("Genres") Musician musician) {
    return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI,
            new String[] {
                    BaseColumns._ID,
                    MediaStore.Audio.GenresColumns.NAME
            }, null, null, null);
}

@Provides
@Named("playListCursor")
Cursor providePlaylistCursor(@Named("Playlist") Musician musician) {
    return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
            new String[] {
                    BaseColumns._ID,
                    MediaStore.Audio.PlaylistsColumns.NAME
            }, null, null, null);
}


}

然后当你想注入时写如下:

@Inject
@Named("soundCursor")
Cursor soundCursor;

@Inject
@Named("albumCursor")
Cursor albumCursor;

@Inject
@Named("artistCursor")
Cursor artistCursor;

@Inject
@Named("genreCursor")
Cursor genreCursor;

@Inject
@Named("playListCursor")
Cursor playListCursor;

如果您想在构造函数注入中注入它们,请执行以下操作:

@Inject
public SomeClassConstructor(@Named("album") Cursor cursur)

还有你在你的子组件接口中写的东西我不明白它是什么,它一定是这样的:

@CursorScope
@Subcomponent(modules = CursorModule.class)
public interface CursorComponent {
   void inject(TheClassThatWantsToUseInject1 obj);
   void inject(TheClassThatWantsToUseInject2 obj);
}

并在您的应用程序组件中:

   YourSubComponentInterface plus(CursorModule module);

所以最后我得到了我自己的问题的答案,它是组件本身,你知道软件开发是如此的忙碌以至于当你退出 cigerrates 时它也会成为更多的负担。

所以以上所有都是一个简单的方法,我用 sharepreference 做了一个不同的例子,因为我的 lappy 在 archlinux 上崩溃了。

下面是我生成的代码片段。

所以我唯一应该做的就是我应该从组件界面中删除游标注入参数。

none 代码段对人们的帮助越小。

分量:-

@Singleton
@Component(modules = {MusicianModule.class, SharedPreferencesModule.class})
public interface MusicianComponent {
    void inject(MainActivity mainActivity);
    Musician musician();
}

模块:-

@Module
public class SharedPreferencesModule {

    @Provides
    @Named("default")
    SharedPreferences provideDefaultsharedPreferences(Musician musician) {
        return musician.getSharedPreferences("default", Context.MODE_PRIVATE);
    }

    @Provides
    @Named("secret")
    SharedPreferences provideSecretsharedPreferences(Musician musician) {
        return musician.getSharedPreferences("secret", Context.MODE_PRIVATE);
    }
}

音乐家模块:-

@Module
public class MusicianModule {

    private Musician musician;

    public MusicianModule(Musician musician) {
        this.musician = musician;
    }

    @Provides @Singleton
    Musician providemusician() {
        return musician;
    }

    @Provides @Singleton
    Application provideapplication(Musician musician) {
        return musician;
    }
}

申请class:-

public class Musician extends Application {

    private MusicianComponent musicianComponent;
    @Override
    public void onCreate() {
        resolvedependency();
        super.onCreate();
    }


    private void resolvedependency() {
        musicianComponent = DaggerMusicianComponent.builder()
                .musicianModule(new MusicianModule(this))
                .sharedPreferencesModule(new SharedPreferencesModule())
                .build();
    }


    public static MusicianComponent getMusicianComponent(Context context) {
        return ((Musician)context.getApplicationContext()).musicianComponent;
    }
}

并在 MainActivity 中注入:-

public class MainActivity extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;



    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    @Inject @Named("default")
    SharedPreferences defSharedPreferences;

    @Inject @Named("secret")
    SharedPreferences secSharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Musician.getMusicianComponent(this).inject(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        defSharedPreferences.edit().putString("status", "worked").apply();
        secSharedPreferences.edit().putString("status", "worked").apply();

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_song, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "SECTION 1";
                case 1:
                    return "SECTION 2";
                case 2:
                    return "SECTION 3";
            }
            return null;
        }
    }