更新到v2.11之后的Dagger2

时间:2017-12-10 00:24:15

标签: android mvvm searchview dagger-2

我更新到dagger v2.11后,我的SearchView组件停止工作。我需要重新安排我的代码吗?我做错了什么?这是我的代码......

    public class TripPlannerApplication extends Application{
    private SearchComponent mSearchComponent;
    @Override
    public void onCreate() {
    super.onCreate();

    mSearchComponent = DaggerSearchComponent.builder()
            .searchModule(new SearchModule())
            .build();

}
}

public static TripPlannerApplication from(@NonNull Context context) {
    return (TripPlannerApplication) context.getApplicationContext();
}

public SearchComponent getSearchComponent() {
    return mSearchComponent;
}
}

我的组件

    @Component(modules = SearchModule.class)
public interface SearchComponent {
    void inject(MyActivity myActivity);
}

我的搜索模块

    @Module
public class SearchModule {
    @Provides
    public ApiManager provideApiManager() {
        return new ApiManager();
    }

    @Provides
    public SearchService provideSearchService() {
        return new SearchService(provideApiManager());
    }

}

最后是我注入代码的方式

public class MyActivity extends AppCompatActivity {
@Inject
SearchService mSearchService;

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    TripPlannerApplication.from(this).getSearchComponent().inject(this);}
抱歉,我忘了把搜索菜单和xml ... 搜索视图没有出现在屏幕上......

这里...... ...

菜单xml ..

   <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:title="@string/action_search_title"
        android:icon="@android:drawable/ic_menu_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="collapseActionView|always"/>
</menu>

这是我的myActivity xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">


    <RelativeLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MyActivity">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/appBarLayout">

            <ProgressBar
                android:id="@+id/progress_bar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />


            <TextView
                android:id="@+id/empty_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:text="@string/no_results"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                android:visibility="gone" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />

        </RelativeLayout>
    </RelativeLayout>
</layout

&GT;

0 个答案:

没有答案