Dagger-2 - 无法访问我的xml资源

时间:2015-11-29 22:13:47

标签: android dagger-2

对不起长问题/代码和我的英语技能:) 我想在我的新Android应用程序中使用Dagger-2。我没有使用Dagger-2或其他依赖库的经验。我想我误解了一些事情......

以下是相关申请。如果活动“OverviewActivity”启动,则会注入“OverviewPresenter”等等。(OverviewActivity-> OverviewPresenter-> CategoriesPovider-> DBOpenHelper-> DBDefaults)。

在第一次启动时,DBDefaults类应该使用一些默认值预填充数据库。当它应该访问资源文件我得到一个NPE。谁能告诉我为什么会这样?我试图使用“日志”工具来检查是否有任何所需的变量为null,但一切看起来都很好。获取DBDefaults类错误的上下文?

最后该方法抛出NPE

context.getResources().getStringArray(R.array.category_colors);

但这是我的代码。 AppComponent.java

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
    Context getContext();
    Application getApplication();
    DBOpenHelper getDBOpenHelper();
}

AppModule.java

@Module
public class AppModule {
    private final Manager app;

    public AppModule(Manager app) {
        this.app = app;
    }

    @Singleton
    @Provides
    public Application provideApplication() {
        return app;
    }

    @Singleton
    @Provides
    public Context provideContext() {
        return app.getApplicationContext();
    }

    @Singleton
    @Provides
    public DBOpenHelper provideDBOpenHelper() {
        return new DBOpenHelper(app);
    }
}

OverviewComponent.java

@ActivityScope
@Component(dependencies = AppComponent.class)
public interface OverviewComponent {
    void inject(OverviewActivity activity);
    OverviewPresenter getOverviewPresenter();
}

OverviewPresenter.java

public class OverviewPresenter extends MvpBasePresenter<OverviewView> {

    @Inject
    public OverviewPresenter(CategoriesProvider provider) {
        Log.d("OverviewPresenter", "presenter..");
    }
}

CategoriesProvider.java

public class CategoriesProvider extends BaseProvider {

    @Inject
    public CategoriesProvider(DBOpenHelper dbOpenHelper) {
        super(dbOpenHelper);
        // Just testing...
        dbOpenHelper.getReadableDatabase();
    }
}

DBOpenHelper.java

public class DBOpenHelper extends SQLiteOpenHelper {

    private static final String db = "manager";
    private static final int version = 1;
    private Context context;

    @Inject
    public DBOpenHelper(Context context) {
        super(context, db, null, version);
        this.context = context;
        Log.d("DBOpenHelper", "database...");
        if (context == null) {
            Log.d("DBOpenHelper", "wtf...");
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // db structure
        db.execSQL(CategoriesTable.getCreateTableQuery());
        db.execSQL(UsersTable.getCreateTableQuery());

        // default values
        DBDefaults defaults = new DBDefaults(db, context);
        defaults.insertSystemUser();
        defaults.insertCategories();
    }
}

DBDefaults.java

public class DBDefaults {
    private SQLiteDatabase db;
    private Context context;

    public DBDefaults(SQLiteDatabase db, Context context) {
        this.db = db;
        this.context = context;
        if (context == null) {
            Log.d("DBDefaults", "wtf... in Defaults");
        }
    }

    public void insertCategories() {
        String[] test = context.getResources().getStringArray(R.array.category_colors);
        for (String item : test) {
            Log.d("Categories", item);
        }
        // NPE ?!?!?!
    }
}

修改 我的Manger课程。在这里,我将存储我的应用程序组件并初始化我的AppModule

public class Manager extends Application {
    private AppComponent appComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        appComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
    }

    public AppComponent getAppComponent() {
        return appComponent;
    }
}

我的活动

public class OverviewActivity extends MvpActivity<OverviewView, OverviewPresenter> {

    public OverviewComponent overviewComponent;

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

    @Override
    public void inject() {
        Log.d("Activity", "inject");
        overviewComponent = DaggerOverviewComponent.builder()
                .appComponent(((MoneyManager)getApplication()).getAppComponent())
                .build();
        overviewComponent.inject(this);
    }

    @Override
    public OverviewPresenter getPresenter() {
        Log.d("Activity", "getPresenter");
        return overviewComponent.getOverviewPresenter();
    }
}

1 个答案:

答案 0 :(得分:0)

如果CategoriesProviderContentProvider,则在致电null时可能会Application.getApplicationContext()。因此,您注入的上下文可能为null。您可以在AppModule中对此进行验证。尝试在返回结果之前记录结果。请注意,Application.getApplicationContext()通常会返回相同的实例,因此您只能return app;而不是当前拥有的内容。