2014-12-19 48 views
0

我試圖定義將使用我的注射類時,似乎不能使用注射詞。Dagger for Android - 無法定義類的注射

這裏是我MainModule.java類:

import dagger.Module; 
import dagger.Provides; 


@Module() 
public class MainModule { 

    private Context context; 

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


    @Provides 
    Object provideSomething(Context context) { 
     return new Object(); 
    } 
} 



@Module() 
class SubModule1 { 

    private Context context; 

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

    @Provides 
    Object provideSomethingElse(Context context) { 
     return new Object(); 
    } 
} 

這裏是我MainApplication類,在android系統擴展應用:

public class MainApplication extends Application { 

    private ObjectGraph objectGraph; 


    @Override 
    public void onCreate() { 
     super.onCreate(); 
     objectGraph = ObjectGraph.create(new MainModule(this)); 
     objectGraph = ObjectGraph.create(new SubModule1(this)); 
     objectGraph = ObjectGraph.create(new ActivityModule()); 
     objectGraph.inject(this); 
} 
    } 

這裏是有跌破發行IM,機器人工作室不會看到injects關鍵字,所以我不能使用它。

的ActivityModule.java類如下:

@Module(
     injects= 
       ListPageActivity.class 

) 
public class ActivityModule { } 

同樣它在ActiveModule的關鍵字內噴射是不是在我的IDE認可。這裏是我的gradle編譯依賴:

dependencies { 
    compile files('libs/volley.jar') 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:21.0.0' 
    compile 'com.android.support:recyclerview-v7:21.0.0' 
    compile 'com.android.support:cardview-v7:21.0.0' 
    compile 'com.jakewharton:butterknife:6.0.0' 
    compile 'com.squareup:dagger:0.9.1' 

} 
+0

只是好奇,爲什麼你用0.9.1而不是最新? – Alex 2014-12-19 05:08:46

+0

我認爲這是我在J中心和maven中心查看的最新版本。你能給我最新的 – j2emanue 2014-12-19 05:26:42

回答

0

改變你的匕首版本。發佈的代碼對我來說似乎都可以。

compile 'com.squareup.dagger:dagger:1.2.2' 

組ID更改,以便知道你可以找到項目Maven的中央here

1

如果您需要使用0.9.1版本,使用entryPoints關鍵字。這是因爲injects被重命名爲entryPoints並存在於更新的版本中。

我建議你使用從廣場的最後版本1.2.2具有injects
http://square.github.io/dagger/

apt "com.squareup.dagger:dagger-compiler:1.2.2" 
compile "com.squareup.dagger:dagger:1.2.2" 

或者最新的匕首2來自谷歌。這仍然是快照,但我希望不會長。:
http://google.github.io/dagger/

apt "com.google.dagger:dagger-compiler:2.0-SNAPSHOT" 
compile "com.google.dagger:dagger:2.0-SNAPSHOT" 
+0

嘿謝謝 - 再加上一個。你能給我在Android Studio中編譯的gradle依賴關係嗎?我無法找到它。我指的是谷歌包裝中的匕首2。 – j2emanue 2014-12-19 15:03:16

+0

你有Maven依賴項目網站。將它轉換爲Gradle很容易,但我已經編輯了答案。 – WojciechKo 2014-12-20 02:59:05

+0

錯誤:「找不到Gradle DSL方法:apt()」 – 2015-02-02 21:39:34

相關問題