Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用顶部工具栏和底部应用程序栏时出错_Android - Fatal编程技术网

Android 使用顶部工具栏和底部应用程序栏时出错

Android 使用顶部工具栏和底部应用程序栏时出错,android,Android,我试图在带有顶部搜索工具栏的RecyclerView的活动中实现底部应用程序栏/导航视图(在其他活动中工作)时,解决一个错误。只是想知道是否有人对如何在一个活动中实现两个工具栏提出了建议 错误如下: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to fal

我试图在带有顶部搜索工具栏的RecyclerView的活动中实现底部应用程序栏/导航视图(在其他活动中工作)时,解决一个错误。只是想知道是否有人对如何在一个活动中实现两个工具栏提出了建议

错误如下:

This Activity already has an action bar supplied by the window decor. Do not request 
Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a 
Toolbar instead.
Toolbar\u menu.xml

<item
    android:id="@+id/action_search"
    android:icon="@mipmap/search"
    android:title="Search"
    app:actionViewClass="android.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView"
    android:layout_gravity="top"
    />

 </menu>


content_browseadverts.xml

    <androidx.constraintlayout.widget.ConstraintLayout 



   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Advert.BrowseAdverts"
    tools:showIn="@layout/activity_browse_adverts">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvBrowseAdverts"
        android:layout_width="409dp"
        android:layout_height="601dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="?attr/actionBarSize"
        />

    <TextView
        android:id="@+id/tvNoAdverts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_records_found"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/coordinator_layout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Advert.BrowseAdverts">

    <include layout="@layout/content_browseadverts" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floating_action_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        app:srcCompat="@drawable/ic_filter"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity"
        tools:layout_editor_absoluteY="81dp"/>
    <androidx.drawerlayout.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">


        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer" />

    </androidx.drawerlayout.widget.DrawerLayout>

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:backgroundTint="@color/colorPrimary"
        app:fabAlignmentMode="center"
        app:navigationIcon="@drawable/ic_menu"
        android:theme="@style/Widget.MaterialComponents.BottomAppBar"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/bar"
        android:src="@drawable/ic_add"/>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

活动\浏览\广告.xml

    <androidx.constraintlayout.widget.ConstraintLayout 



   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Advert.BrowseAdverts"
    tools:showIn="@layout/activity_browse_adverts">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvBrowseAdverts"
        android:layout_width="409dp"
        android:layout_height="601dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="?attr/actionBarSize"
        />

    <TextView
        android:id="@+id/tvNoAdverts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_records_found"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/coordinator_layout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Advert.BrowseAdverts">

    <include layout="@layout/content_browseadverts" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floating_action_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        app:srcCompat="@drawable/ic_filter"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity"
        tools:layout_editor_absoluteY="81dp"/>
    <androidx.drawerlayout.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">


        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer" />

    </androidx.drawerlayout.widget.DrawerLayout>

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:backgroundTint="@color/colorPrimary"
        app:fabAlignmentMode="center"
        app:navigationIcon="@drawable/ic_menu"
        android:theme="@style/Widget.MaterialComponents.BottomAppBar"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/bar"
        android:src="@drawable/ic_add"/>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

BrowseAdverts.java

    private DatabaseHelper myDb;

    private AdvertsAdapter mAdapter;
    private List<Advert> advertsList = new ArrayList<>();
    private CoordinatorLayout coordinatorLayout; //why is this saying its not used?
    private TextView noAdvertsView;
    private RecyclerView recyclerView;
    private Button btnFilter;

    Toolbar toolbar2;

    private NavigationView navigationView;
    private DrawerLayout drawer;
    private View navHeader;
    private ImageView imgNavHeaderBg, imgProfile;
    private TextView txtName, txtWebsite;
    private BottomAppBar bottomAppBar;
    private String[] activityTitles;
    private Handler mHandler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppThemeToolbar);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browse_adverts);

        coordinatorLayout = findViewById(R.id.coordinator_layout);
        recyclerView = findViewById(R.id.rvBrowseAdverts);
        noAdvertsView = findViewById(R.id.tvNoAdverts);

        final FloatingActionButton floatingActionButton =
                (FloatingActionButton) findViewById(R.id.floating_action_button);


        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CountySheet bottomSheet = new CountySheet();
                bottomSheet.show(getSupportFragmentManager(), "countySheet");
            }

        });

        myDb = new DatabaseHelper(this);

        advertsList.addAll(myDb.getAllAdverts());

        mAdapter = new AdvertsAdapter(this, advertsList, this);
        RecyclerView.LayoutManager mLayoutManager = new 
      LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(this, 
      DividerItemDecoration.VERTICAL));

        recyclerView.setAdapter(mAdapter);

        toggleEmptyAdverts();

        bottomAppBar = findViewById(R.id.bar);
        setSupportActionBar(bottomAppBar);
        mHandler = new Handler();
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        navigationView = (NavigationView) findViewById(R.id.nav_view);

        navHeader = navigationView.getHeaderView(0);
        txtName = (TextView) navHeader.findViewById(R.id.name);
        txtWebsite = (TextView) navHeader.findViewById(R.id.website);
        imgNavHeaderBg = (ImageView) navHeader.findViewById(R.id.img_header_bg);
        imgProfile = (ImageView) navHeader.findViewById(R.id.img_profile);

        activityTitles = getResources().getStringArray(R.array.nav_item_activity_titles);

        loadNavHeader();

        setUpNavigationView();

        if (savedInstanceState == null) {
            navItemIndex = 0;
            CURRENT_TAG = TAG_HOME;
            loadHomeFragment();
        }

        FloatingActionButton fab = findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(BrowseAdverts.this, CreateAdvert.class);
                startActivity(intent);
            }
        });

    }

    private void toggleEmptyAdverts() {

        if (myDb.getAdvertCount() > 0) {
            noAdvertsView.setVisibility(View.GONE);
        } else {
            noAdvertsView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_menu, menu);

        MenuItem menuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) menuItem.getActionView();

        searchView.setOnQueryTextListener(this);

        if (navItemIndex == 0) {
            // getMenuInflater().inflate(R.menu.main, menu);
            getMenuInflater().inflate(R.menu.bottom_bar_menu, menu);

        }
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {


        String userInput = newText.toLowerCase();

        ArrayList<Advert> newList = new ArrayList<>();

        for (Advert advert: advertsList) {
            String name = advert.getAdvertName().toLowerCase();

            if (name.contains(userInput)){
                newList.add(advert);
            }

            mAdapter.setFilter(newList);

        }
        return true;

    }

    @Override
    public void onAdvertClick(int position) {
        Intent intent = new Intent (this, AdvertDetail.class);
        intent.putExtra("selected_advert" , advertsList.get(position));

        startActivity(intent);

    }

    private void loadNavHeader() {
        SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", 
      Context.MODE_PRIVATE);
        String firstName = sharedPreferences.getString("firstName", "default");
        String lastName = sharedPreferences.getString("lastname", "default");
        txtName.setText(firstName + " " + lastName);
        String email = sharedPreferences.getString("email", "default");
        txtWebsite.setText(email);
        String id = sharedPreferences.getString("id", "default");
        int userid = Integer.parseInt(id);
        imgProfile.setImageBitmap(myDb.getImage(userid));
    }

    private void loadHomeFragment() {
        selectNavMenu();

        setToolbarTitle();

        if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null) {
            drawer.closeDrawers();

            return;
        }

        Runnable mPendingRunnable = new Runnable() {
            @Override
            public void run() {
                FragmentTransaction fragmentTransaction = 
      getSupportFragmentManager().beginTransaction();
                fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
                        android.R.anim.fade_out);
                fragmentTransaction.commitAllowingStateLoss();
            }
        };

        if (mPendingRunnable != null) {
            mHandler.post(mPendingRunnable);
        }

        drawer.closeDrawers();

        invalidateOptionsMenu();
    }

    private void setToolbarTitle() {
        getSupportActionBar().setTitle(activityTitles[navItemIndex]);
    }

    private void selectNavMenu() {
        navigationView.getMenu().getItem(navItemIndex).setChecked(true);
    }

    private void setUpNavigationView() {
        navigationView.setNavigationItemSelectedListener(new 
      NavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {

                switch (menuItem.getItemId()) {
                    case R.id.nav_home:
                        navItemIndex = 0;
                        Intent intent = new Intent(BrowseAdverts.this, MainActivity.class);
                        startActivity(intent);
                        return true;
                    case R.id.nav_adverts:
                        navItemIndex = 1;
                        Intent intent2 = new Intent(BrowseAdverts.this, BrowseAdverts.class);
                        startActivity(intent2);
                        return true;
                    case R.id.nav_savedads:
                        navItemIndex = 2;
                        Intent intent3 = new Intent(BrowseAdverts.this, FavouriteAdverts.class);
                        startActivity(intent3);
                        return true;
                    case R.id.nav_notifications:
                        navItemIndex = 3;
                        Intent intent4 = new Intent(BrowseAdverts.this, BrowseUsers.class);
                        startActivity(intent4);
                        return true;
                    case R.id.nav_profile:
                        navItemIndex = 4;
                        Intent intent5 = new Intent(BrowseAdverts.this, MyProfile.class);
                        startActivity(intent5);
                        return true;
                    case R.id.nav_contact:
                        // launch new intent instead of loading fragment
                        startActivity(new Intent(BrowseAdverts.this, MainActivity.class));
                        drawer.closeDrawers();
                        return true;

                    case R.id.nav_qa:
                        startActivity(new Intent(BrowseAdverts.this, MainActivity.class));
                        drawer.closeDrawers();
                        return true;
                    case R.id.nav_logout:
                        SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
                        sharedPreferences.edit().remove("text").commit();
                        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedpreferences.edit();
                        editor.commit();
                        startActivity(new Intent(BrowseAdverts.this, Login.class));
                        drawer.closeDrawers();
                        return true;
                    default:
                        navItemIndex = 0;
                }
                if (menuItem.isChecked()) {
                    menuItem.setChecked(false);
                } else {
                    menuItem.setChecked(true);
                }
                menuItem.setChecked(true);

                loadHomeFragment();

                return true;
            }
        });

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, 
     bottomAppBar, R.string.openDrawer, R.string.closeDrawer) {

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };

        drawer.setDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
    }

  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
            case R.id.app_bar_profile:
                Intent intent = new Intent(BrowseAdverts.this, MyProfile.class);
                startActivity(intent);
                return true;
            }
           return super.onOptionsItemSelected(item);
          }
        }
私有数据库助手myDb;
私人广告;
private List advertsList=new ArrayList();
私人协调人布局协调人布局//为什么这句话不用呢?
私有文本视图noAdvertsView;
私人回收站;
专用按钮BTN过滤器;
工具栏2;
私有导航视图导航视图;
私人抽屉;
私有视图导航头;
私有图像视图imgNavHeaderBg,imgProfile;
私有文本视图txtName,TXT网站;
私人BottomAppBar BottomAppBar;
私有字符串[]活动标题;
私人经理人;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
setTheme(R.style.AppThemeToolbar);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u browse\u广告);
coordinatorLayout=findViewById(R.id.coordinator\U布局);
recyclerView=findViewById(R.id.rvBrowseAdverts);
noAdvertsView=findViewById(R.id.tvNoAdverts);
最终浮动操作按钮浮动操作按钮=
(浮动操作按钮)findViewById(R.id.浮动操作按钮);
floatingActionButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
CountySheet bottomSheet=新CountySheet();
show(getSupportFragmentManager(),“countySheet”);
}
});
myDb=新数据库助手(此);
addAll(myDb.getAllAdverts());
mAdapter=新广告dapter(this,advertsList,this);
RecyclerView.LayoutManager mLayoutManager=新建
LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
setItemAnimator(新的DefaultItemAnimator());
recyclerView.addItemDecoration(新的DividerItemDecoration)(此,
DividerItemDecoration.VERTICAL);
recyclerView.setAdapter(mAdapter);
切换EmptyAdverts();
bottomAppBar=findViewById(R.id.bar);
设置支持操作栏(底部应用栏);
mHandler=新处理程序();
抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
navigationView=(navigationView)findViewById(R.id.nav_视图);
navHeader=navigationView.getHeaderView(0);
txtName=(TextView)navHeader.findViewById(R.id.name);
txtWebsite=(TextView)navHeader.findviewbyd(R.id.website);
imgNavHeaderBg=(ImageView)navHeader.findviewbyd(R.id.img\u header\u bg);
imgProfile=(ImageView)navHeader.findViewById(R.id.img_profile);
activityTitles=getResources().getStringArray(R.array.nav\u item\u activity\u titles);
loadNavHeader();
setUpNavigationView();
如果(savedInstanceState==null){
navItemIndex=0;
当前标签=标签首页;
loadHomeFragment();
}
FloatingActionButton fab=findViewById(R.id.fab);
fab.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
Intent Intent=新Intent(BrowseAdverts.this,CreateAdvert.class);
星触觉(意向);
}
});
}
私有void toggleEmptyAdverts(){
如果(myDb.getAdvertCount()>0){
noAdvertsView.setVisibility(View.GONE);
}否则{
noAdvertsView.setVisibility(View.VISIBLE);
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.toolbar\u菜单,菜单);
MenuItem MenuItem=menu.findItem(R.id.action\u search);
SearchView SearchView=(SearchView)menuItem.getActionView();
setOnQueryTextListener(这个);
如果(navItemIndex==0){
//getMenuInflater().充气(R.menu.main,menu);
getMenuInflater().充气(右菜单。底部菜单\u栏\u菜单,菜单);
}
返回true;
}
@凌驾
公共布尔值onQueryTextSubmit(字符串查询){
返回false;
}
@凌驾
公共布尔onQueryTextChange(字符串newText){
字符串userInput=newText.toLowerCase();
ArrayList newList=新的ArrayList();
for(广告:广告列表){
字符串名称=advert.getAdvertName().toLowerCase();
if(name.contains(userInput)){
添加(广告);
}
mAdapter.setFilter(新列表);
}
返回true;
}
@凌驾
dvertclick上的公共无效(int位置){
Intent Intent=新的Intent(这个,adverdetail.class);
intent.putExtra(“选中的广告”,广告列表.get(位置));
星触觉(意向);
}
私有void loadNavHeader(){
SharedReferences SharedReferences=GetSharedReferences(“MyPrefs”,
上下文。模式(私人);
String firstName=SharedReferences.getString(“firstName”,“default”);
String lastName=SharedReferences.getString(“lastName”,“default”);
txtName.setText(firstName+“”+lastName);
String email=SharedReferences.getString(“电子邮件”,“默认”);
txtWebsite.setText(电子邮件);
String id=SharedReferences.getString(“id”,“default”);
int userid=Integer.parseInt(id);
setImageBitmap(myDb.getImage(userid));