我正在使用BaseActivity实现的使用多个活动(不使用片段)实现MaterialDrawer(库MikePenz)的问题。另外,使用ButterKnife绑定视图。使用具有BaseActivity/Child Activities和MaterialDrawer的Butterknife
在SO中发现了相关问题,并逐个尝试了一下,但在我的情况下它并没有工作,因为它们大多数使用标准导航抽屉碎片或者它们与ButterKnife没有关系。
代码:
MainActivity类别
public class MainActivity extends AppCompatActivity {
@BindView(R.id.toolbar)
Toolbar toolbar;
private List<TestModel> destinations;
private DestinationAdapter mAdapter;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
navManagement();
}
}
activity_main布局
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/maincontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tvs.ui.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
/>
</android.support.design.widget.AppBarLayout>
</android.support.constraint.ConstraintLayout>
StoreActivity
public class StoresActivity extends MainActivity {
@BindView(R.id.searchEditText)
EditText mSearchEditText; // errorRequired view 'searchEditText' with ID 2131558557 for field 'mSearchEditText' was not found
@BindView(R.id.storesRecyclerView)
RecyclerView recyclerView; // here too
private List<TestModel> stores;
private StoreAdapter mAdapter;
//endregion
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stores);
ButterKnife.bind(this);
loadRecyclerView();
testData();
}
}
activity_store布局
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/searchEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/storesRecyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
我知道错误的原因是当布局被夸大但视图没有在继承的活动中膨胀时,MainActivity中的ButterKnife.Bind被调用。导航抽屉会加载,但点击项目时会失败,原因很明显。我不能使用FrameLayout,因为我使用的MaterialDrawer库没有(或者我不知道)有膨胀的视图。
我试了好几种方法,广泛搜索等等之类SO1SO2GitGit2 Link,但没有成功。
感谢任何帮助,深表感谢。提前致谢。
其中声明了activity_main? –
@LOG_TAG请检查问题,activity_main布局以及代码是否存在。 – user2695433
我在上面的代码BaseActivity询问你声明setContentView(getLayoutResourceId());你从哪里加载activity_main,因为你没有调用setContentView(R.layout.activity_main);在BaseActivity中(上面的代码)任何完全正常工作的要点? –