ButterKnife注射有问题。启动我的应用后,我收到以下错误:
01-23 11:35:36.229 23663-23663/lt.andro.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: lt.andro.myapp, PID: 23663
java.lang.RuntimeException: Unable to inject views for MyNewFragment{528796ec #0 id=0x7f0a017c android:switcher:2131362172:1}
at butterknife.ButterKnife.inject(ButterKnife.java:263)
at butterknife.ButterKnife.inject(ButterKnife.java:207)
at lt.andro.myapp.BaseFragment.onViewCreated(BaseFragment.java:66)
at lt.andro.myapp.MyNewFragment.onViewCreated(MyNewFragment.java:117)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:961)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.
看起来所有ButterKnife的@InjectView类型都是正确的,但仍然ButterKnife无法为我的新片段注入视图。
我的布局看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
我的新片段有@InjectViews:
@InjectView(R.id.swipe_refresh) SwipeRefreshLayout swipeRefreshLayout;
@InjectView(R.id.recycler) RecyclerView list;
答案 0 :(得分:1)
在片段中,您应该在onCreateView(...)
中使用它View v = inflater.inflate(R.layout.meh, container, false);
ButterKnife.inject(this, v);
答案 1 :(得分:0)
显然我必须检查其他ButterKnife的注释,如@OnItemClick
。我已将它用于RecyclerView,这对我造成了RuntimeException。
答案 2 :(得分:0)
在注入的视图中监视您正在注入的任何绑定也会付出代价。
在我的实例中,我有一个片段 foo ,它注入了一个视图 bar
// Foo.java
View v = inflater.inflate(R.layout.bar, container, false);
ButterKnife.inject(this, v);
这引发了所描述的错误。事实证明,在 foo 中我绑定了 baz 中的一个元素,我希望在我的注入视图 bar 中找到它。
// Foo.java
@InjectView(R.id.baz)
TextView filterTextView;
除了我在'baz&#39;在布局xml中 bar
// bar.xml
<EditText
android:id="@+id/bazz"
/>
这导致Unable to inject views for...