Android: use ButterKnife in action, expanding from other activity

I need a navigation box in all my actions. Therefore, I used the BaseActivity for Navigation box and expanded other actions from the base activity. Base activity has navigation boxes. Toolbar activity extends the underlying activity, but it throws an exception when I try to use butterknife to bind views that say

java.lang.IllegalStateException: Required view 'dashboard_frameLayout' with ID 2131558517 for field 'frameLayout' was not found. 

here are the relevant files
BaseActivity.java

 public class BaseActivity extends AppCompatActivity { @BindView(R.id.toolbar) Toolbar toolbar; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_base); ButterKnife.bind(this); //set toolbar and both Navigation Drawer } 

DashboardActivity.java

 public class DashBoardActivity extends BaseActivity { @BindView(R.id.dashboard_frameLayout) FrameLayout frameLayout; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); View view = getLayoutInflater().inflate(R.layout.activity_dashboard,frameLayout); ButterKnife.bind(this,view); init(); } private void init() { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dashboard_frameLayout); if(fragment != null){ Utils.getInstance().addFragment(this,new Fragment_Dashboard(),R.id.dashboard_frameLayout); } } } 

activity_dashboard.xml

 <FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dashboard_frameLayout" android:layout_height="match_parent" android:layout_width="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

why the framelayout function was not detected in the activity of the dashboard?

+3
android android-activity exception butterknife
source share
3 answers

Wrong, I did not carefully check the question.

Edited by:

In your example, you bound views twice. One in BaseActivity and the other in DashBoardActivity.

  • No need to do this in BaseActivity. But make sure the subclass has a toolbar with id R.id.toolbar.
  • No need to inflate the layout of R.layout.activity_dashboard, you did it in setContentView. It should be like this:

     @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dashboard); ButterKnife.bind(this); init(); } 

After running ButterKnife.bind (this), frameLayout is ready to use. Verify that the frameelayout id dashboard_frameLayout exists in your activity layout file.

0
source share

In your case, "ButterKnife.bind (this)" (from onCreate () from BaseActivity) is called before OnCreate () from DashBoardActivity. Therefore, when ButterKnife tries to link activity_base views, it cannot find an identifier of the form R.id.dashboard_frameLayout in the activity_base layout that throws an exception.

To bind a view, you must add a container, such as a view group, such as RelativeLayout, to Activity_base. Create the addViews () method in BaseActivity. Then add a layout of the activity_dashboard view to this container from onCreate () DashBoardActivity by calling addViews () from Baseactivity. Now you can add โ€œButterKnife.bind (this)โ€ to the addViews () method (and you shouldn't call ButterKnife.bind anywhere else in both steps). Please note that here you should get a reference to the container view identifier in BaseActivity, simply using findViewById, other types will get ButterKnife binding.

0
source share

I did something similar in my code.

My goal is to have a basic box with one box, which I reuse in different actions, so I want to be able to use the activity_base_drawer layout in all actions, but change the content, inflating new views.

in my base activity (which is the activity of the navigation box), I have this protected method

 protected void inflateContent(@LayoutRes int inflatedResID){ setContentView(R.layout.activity_base_drawer); LinearLayout contentContainer = ButterKnife.findById(this, R.id.content_container); getLayoutInflater().inflate(inflatedResID, contentContainer); ButterKnife.bind(this); setSupportActionBar(toolbar); setupNavDrawer(); } 

I send the layout of the base navigation box to setContentView() , which has a linear layout, which is the container that I want to inflate new layouts in it for each action, which extends my base activity

As you can see, LinearLayout contentContainer uses findById not @BindView . I have to do it this way because I need to inflate the view in the content container before I call ButterKnife.bind(this);

After finding the layout of the content container, I inflate it using getLayoutInflater().inflate(inflatedResID, contentContainer);

Then I call ButterKnife.bind(this);

Finally, some customization methods depend on layouts that are associated with @BindView in the base activity class.

and in any expanding action onCreate () looks like where I send the id of the layout I want to inflate to inflateContent()

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); inflateContent(R.layout.content_main); } 
0
source share

All Articles