java.lang.IllegalStateException 黄油刀

java.lang.IllegalStateException Butterknife

我正面临 java.lang.IllegalStateException Required view 'splash_text' 但我已将其包含在 xml 中。

我正在使用 Butterknife 绑定视图。

 compile 'com.jakewharton:butterknife:7.0.1'

Xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_light">

<com.CustomTextView
    android:id="@+id/splash_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   />

</RelativeLayout>

Activity :

@Bind(R.id.splash_text)
CustomTextView mSplashTv;

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

    setContentView(R.layout.activity_splash);
    ButterKnife.bind(this);

    mSplashTv.setText("Splash");

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            finishSplash();
        }
    },3000);
}

应用程序在第 mSplashTv.setText("Splash");

行崩溃

日志:

  Caused by: java.lang.IllegalStateException: Required view 'splash_text' with ID 2131492944 for field 'mSplashTv' was not found. If this view is optional add '@Nullable' annotation.
  at butterknife.ButterKnife$Finder.findRequiredView(ButterKnife.java:140)
  at com.sonymix.activities.SplashActivity$$ViewBinder.bind(SplashActivity$$ViewBinder.java:12)
  at com.sonymix.activities.SplashActivity$$ViewBinder.bind(SplashActivity$$ViewBinder.java:9)
  at butterknife.ButterKnife.bind(ButterKnife.java:319)
  at butterknife.ButterKnife.bind(ButterKnife.java:237) 
  at com.sonymix.activities.BaseActivity.onCreate(BaseActivity.java:16) 
  at com.sonymix.activities.SplashActivity.onCreate(SplashActivity.java:33) 
  at android.app.Activity.performCreate(Activity.java:5372) 
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) 
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267) 
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359) 
  at android.app.ActivityThread.access0(ActivityThread.java:165) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326) 
  at android.os.Handler.dispatchMessage(Handler.java:99) 
  at android.os.Looper.loop(Looper.java:137) 
  at android.app.ActivityThread.main(ActivityThread.java:5455) 
  at java.lang.reflect.Method.invokeNative(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:525) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
  at dalvik.system.NativeStart.main(Native Method) 

更新:

基础Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
}

使用的支持库:

 compile 'com.android.support:appcompat-v7:23.1.1'

检查您的支持库版本。自从我将支持库升级到 23 版本后,我收到了上述错误。可能是导致错误的原因。

从基础中删除绑定 activity。当 ButterKnife 在基础 class 中绑定时,您的布局尚未膨胀。
您还可以向字段添加 @Nullable 注释。

这是因为你的布局还没有膨胀。如果您正在使用 BaseActivity,请执行以下操作:

public abstract class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(this.getLayoutContentViewID());
        ButterKnife.bind(this); //Configure Butterknife
    }

    public abstract int getLayoutContentViewID();

}

接下来,在您的 MainActivity 中:

public class MainActivity extends BaseActivity {

    @BindView(R.id.main_activity_coordinator_layout) CoordinatorLayout coordinatorLayout;

    ...

    @Override
    public int getLayoutContentViewID() { return R.layout.activity_main; }
    ...

}