栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

所以最后我得到了我自己的问题的答案,它本身就是组件的一部分,您知道软件开发太忙了,以至于当您退出公司时,它也成为了更大的负担。

因此,以上所有方法都是一种简单的方法,我用sharepreference制作了另一个示例,因为我的笔记本电脑在archlinux上崩溃了。

所以这是我生成的代码的片段。

所以我唯一要做的就是应该从组件接口中删除光标注入参数。

摘录可能会帮助人们。

零件 :

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

模块:

@Modulepublic 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);    }}

音乐家模块:

@Modulepublic 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;    }}

应用类别:

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 {        private SectionsPagerAdapter mSectionsPagerAdapter;        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);    }        public static class PlaceholderFragment extends Fragment {                private static final String ARG_SECTION_NUMBER = "section_number";        public PlaceholderFragment() {        }                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;        }    }        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;        }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/515411.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号