8

I am facing java.lang.IllegalStateException Required view 'splash_text' but I have included it in the xml.

I am using Butterknife to Bind the views.

 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);
}

Application is crashing at the line mSplashTv.setText("Splash");

Log :

  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.access$700(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) 

Update:

BaseActivity:

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

Support Library used:

 compile 'com.android.support:appcompat-v7:23.1.1'
6
  • Please post onCreate of BaseActivity.
    – Rohit5k2
    Commented Mar 3, 2016 at 11:26
  • Possible duplicate of NavigationView get/find header layout
    – Shruti
    Commented Mar 3, 2016 at 11:29
  • Try removing ButterKnife.bind(this); from BaseActivity.
    – Rohit5k2
    Commented Mar 3, 2016 at 11:32
  • I tried. Still facing same issue. Commented Mar 3, 2016 at 11:35
  • Not sure but smell like support library dependency issue and show your base application Commented Mar 3, 2016 at 11:35

3 Answers 3

10

Remove binding from base activity. When ButterKnife bind in base class, your layout not inflated yet.
Also you can add @Nullable annotation to field.

1
  • 1
    Adding the @Nullable to the field fixed the issue for me.
    – Goke Obasa
    Commented Apr 6, 2018 at 12:27
3

It's because your layout is not inflated yet. If you are using BaseActivity, do something like this :

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();

}

Next, in your MainActivity :

public class MainActivity extends BaseActivity {

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

    ...

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

}
0

Check your support Library version. I was getting the above error since i upgraded support library to 23 version. may be that is causing error.NavigationView get/find header layout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.