Reputation: 39
I've used CoordinatorLayout + AppBarLayout so the Toolbar could disappear while scrolling and to put Recycler View below tablayout. But after I've changed model logic and added child elements in the toolbar this fatal error keeps happening. After changing the root layout to LinearLayout or FrameLayout the error disappears, but I don't get desired behavior. I don't call, create or change my Layout params for AppBarLayout and CoordinatorLayout in the code (MainActivity). So the stack trace does not show me any internal lines of code, just the internal classes logic. Here is the XML:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:contentInsetStart="0dp"
app:layout_scrollFlags="scroll|snap"
app:titleTextColor="@android:color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/toolbarBackButton"
android:layout_width="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:visibility="invisible"/>
<TextView
android:id="@+id/toolbarText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:fontFamily="sans-serif-medium"
android:gravity="center_vertical"
android:text="@string/activity_main_toolbar_title"
android:textColor="@android:color/white"
android:textSize="20sp" />
<ImageView
android:id="@+id/toolbarBucket"
android:layout_width="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@color/colorPrimary"
app:tabIndicatorColor="@color/colorIndicator"
app:tabSelectedTextColor="@color/colorActive"
app:tabTextColor="@color/colorInactive" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_anchor="@id/tabs"
app:layout_anchorGravity="bottom"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="@dimen/regularMargin"
android:src="@drawable/ic_baseline_add_24"
app:tint="@android:color/white"/></androidx.coordinatorlayout.widget.CoordinatorLayout>
Any clues will be much appreciated! My main activity code
package com.example.loftmoney.screens.main;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import android.content.Intent;
import android.os.Bundle;
import com.example.loftmoney.R;
import com.example.loftmoney.model.Item;
import com.example.loftmoney.screens.additem.AddItemActivity;
import com.example.loftmoney.screens.balance.BalanceFragment;
import com.example.loftmoney.screens.budget.BudgetFragment;
import com.example.loftmoney.screens.main.adapter.FragmentItem;
import com.example.loftmoney.screens.main.adapter.MainPagerAdapter;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements MainClickAdapter, EditModeListener {
private ViewPager2 viewPager;
private Toolbar toolbar;
TabLayout tabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupFab();
setupTabs();
setupToolbar();
}
private void setupToolbar() {
toolbar = findViewById(R.id.toolbar);
}
private void setupTabs() {
List<FragmentItem> fragments = setupFragments();
tabs = findViewById(R.id.tabs);
viewPager = findViewById(R.id.viewPager);
MainPagerAdapter adapter = new MainPagerAdapter(fragments, this, 0);//behavior?
viewPager.setOffscreenPageLimit(fragments.size());
viewPager.setAdapter(adapter);
new TabLayoutMediator(tabs, viewPager,
(tab, position) -> tab.setText(
fragments.get(position).getTitle()
)
).attach();
}
private List<FragmentItem> setupFragments() {
List<FragmentItem> fragments = new ArrayList();
fragments.add(new FragmentItem(new BudgetFragment(), getString(R.string.expenses), Item.ItemType.EXPENSE));
fragments.add(new FragmentItem(new BudgetFragment(), getString(R.string.incomes), Item.ItemType.INCOME));
fragments.add(new FragmentItem(new BalanceFragment(), getString(R.string.balance)));
return fragments;
}
private Fragment getActiveFragment() {
final int activeFragmentIndex = viewPager.getCurrentItem();
Fragment activeFragment = (BudgetFragment) getSupportFragmentManager().getFragments().get(activeFragmentIndex);
return activeFragment;
}
private void setupFab() {
FloatingActionButton addFab = findViewById(R.id.add_fab);
addFab.setOnClickListener(v -> onFabClick());
}
@Override
public void onFabClick() {
BudgetFragment activeFragment = (BudgetFragment) getActiveFragment();
Intent intent = new Intent(MainActivity.this, AddItemActivity.class).putExtra("type", activeFragment.type);
activeFragment.startActivityForResult(intent, BudgetFragment.LAUNCH_ADD_ITEM);
}
@Override
public void onEditModeChangeListener(boolean status) {
toolbar.setBackgroundColor(getApplicationContext().getColor(
status ? R.color.editModeColor : R.color.colorPrimary));
}
}
Upvotes: 0
Views: 1048
Reputation: 39
This was because I've set up an anchor. I've deleted these 2 lines and all works fine
app:layout_anchor="@id/tabs"
app:layout_anchorGravity="bottom"
Upvotes: 0