2017-03-02 8 views
0

1 - Gültigkeitsbereich:Was ist falsch an diesem einfachen dagger2-Beispiel?

@Scope 
@Retention(RetentionPolicy.CLASS) 
public @interface PerInstance {} 

2 - AppContextModule:

@Module 
public class AppContextModule { 

Application application; 

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

@Provides 
public Application application(){ 
    return this.application; 
} 

@Provides 
public Context context(){ 
    return this.application; 
} 

@Provides 
public LocationManager locationManager(Context context){ 
    return (LocationManager)  context.getSystemService(context.LOCATION_SERVICE); 
} 

} 

3 - MeuPrimeiroModule:

@Module 
public class MeuPrimeiroModule { 

@Provides 
@PerInstance 
public String nome() { 
    return new String("Gorick"); 
} 

} 

4 - MeuSegundoModule:

@Module(includes = MeuPrimeiroModule.class) 
public class MeuSegundoModule { 

@Provides 
@Singleton 
public String nomeCompleto(MeuPrimeiroModule meuPrimeiroModule) { 
    return new String(meuPrimeiroModule + " Silva"); 
} 

} 

5 - MeuPrimeiroComponent:

@PerInstance 
@Component(modules={MeuPrimeiroModule.class}) 
public interface MeuPrimeiroComponent { 
void inject(MainActivity mainActivity); 
} 

6 - MeuSegundoComponent:

@Singleton 
@Component(modules={MeuSegundoComponent.class}) 
public interface MeuSegundoComponent extends MeuPrimeiroComponent { 

void inject(MainActivity mainActivity); 
} 

7 - AppContextComponent:

public interface AppContextComponent { 

Application app(); //provision method 
Context applicationContext(); //provision method 
LocationManager locationManager(); //provision method 
} 

8 - ApplicationComponent:

@Singleton 
@Component(modules={AppContextModule.class}) 
public interface ApplicationComponent extends AppContextComponent { 
void inject(MainActivity mainActivity); 
} 

9 - MainActivity:

public class MainActivity extends AppCompatActivity { 

@Inject 
MeuPrimeiroComponent meuPrimeiroComponent; 

@Inject 
MeuSegundoComponent meuSegundoComponent; 

TextView nome, nomeCompleto; 


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

    nome = (TextView) findViewById(R.id.nome); 
    nomeCompleto = (TextView) findViewById(R.id.nomeCompleto); 

    setNome(nome, nomeCompleto); 
} 

public void setNome(TextView nome, TextView nomeCompleto){ 
    nome.setText(meuPrimeiroComponent.toString()); 
    nomeCompleto.setText(meuSegundoComponent.toString()); 
} 

} 

Körperbau:

Error:(16, 10) error: gorick.dagger2.Dagger2.Component.MeuPrimeiroComponent cannot be provided without an @Provides- or @Produces-annotated method. 
gorick.dagger2.Dagger2.Component.MeuPrimeiroComponent is injected at 
gorick.dagger2.MainActivity.meuPrimeiroComponent 
gorick.dagger2.MainActivity is injected at 
gorick.dagger2.Dagger2.Component.ApplicationComponent.inject(mainActivity) 

PS: Wenn ich meuPremeiroComponent.nome() verwende, findet Android Studio die Methode nome() nicht.

+0

Von wo aus injizieren Sie 'MainActivity'? –

+0

Von MeuPrimeiroComponent. –

Antwort

0

Sie können keine Werte in der Komponente injizieren, anstatt Sie in Objekt, dessen Konstruktor enthält Werte injizieren müssen, die Renditen durch die Anbieter

Für weitere Informationen Ref dieses Example

Sie bauen wie diese Komponenten müssen auf onCreate() ist:

DaggerMeuPrimeiroComponent.builder() 
       // list of modules that are part of this component need to be created here too 
       .appContextModule(new AppContextModule(getApplicationContext())) // This also corresponds to the name of your module: %component_name%Module 
       .build().inject(this); 
+0

Ich habe deine Antwort nicht sehr gut verstanden. –

+0

Ich habe meine Antwort bearbeitet und Ihnen das Beispiel gegeben –