gpt4 book ai didi

java - 当 fragment 膨胀时,抽屉导航启动未知 Activity

转载 作者:行者123 更新时间:2023-12-02 03:35:48 25 4
gpt4 key购买 nike

使用 getSupportFragmentManager().beginTransaction().replace() 方法从一个 fragment (PharmaciesFragment,这是一个谷歌地图 fragment )切换到另一个 fragment (ShoppingCartFragment,有问题的 fragment )时,它会膨胀 ShoppingCartFragment,然后启动一个未知的 Activity

MenuActivity 代码

public class MenuActivity extends AppCompatActivity implements 
NavigationView.OnNavigationItemSelectedListener {

Toolbar toolbar;
DrawerLayout drawerLayout;
NavigationView navigationView;
LinearLayout navHeaderLayout;
View headerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
initializeWidgetsAndListeners(savedInstanceState);
}

private void initializeWidgetsAndListeners(Bundle savedInstanceState) {
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);

drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();

navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new PharmaciesFragment()).commit();
navigationView.setCheckedItem(R.id.map);
}
headerView = navigationView.getHeaderView(0);

}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.map:
if (!navigationView.getMenu().getItem(item.getOrder()).isChecked()) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new PharmaciesFragment()).commit();
}
break;
case R.id.shopping_cart:
if (!navigationView.getMenu().getItem(item.getOrder()).isChecked()) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ShoppingCartFragment()).commit();
finish();
}
case R.id.profile:
intent = new Intent(this, ProfileActivity.class);
startActivity(intent);
finish();
break;
case R.id.settings:
intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
finish();
break;

}

drawerLayout.closeDrawer(GravityCompat.START);
return true;
}

}

ShoppingCartFragment 代码

package com.example.pharmaship;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class ShoppingCartFragment extends Fragment {
View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_shopping, null, false);
return view;
}
}

fragment_shopping.xml 代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_centerInParent="true"
android:text="MESSAGE"/>

</RelativeLayout>

activity_menu 代码

<?xml version="1.0" encoding="utf-8"?>
<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:context=".MenuActivity"
tools:openDrawer="start">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/white"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

tools:targetApi="lollipop" >
<ImageView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:layout_gravity="start"
android:layout_marginLeft="20dp"
android:id="@+id/toolbar_logo"
android:layout_marginStart="85dp" />
</androidx.appcompat.widget.Toolbar>

<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

<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"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

最佳答案

尝试删除 onNavigationItemSelected 监听器中的 finish() 方法

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.map:
if (!navigationView.getMenu().getItem(item.getOrder()).isChecked()) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new PharmaciesFragment()).commit();
}
break;
case R.id.shopping_cart:
if (!navigationView.getMenu().getItem(item.getOrder()).isChecked()) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ShoppingCartFragment()).commit();
finish(); // <-- REMOVE THIS LINE
}
break; // And add the break instruction
case R.id.profile:
intent = new Intent(this, ProfileActivity.class);
startActivity(intent);
finish();
break;
case R.id.settings:
intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
finish();
break;

}

drawerLayout.closeDrawer(GravityCompat.START);
return true;
}```

关于java - 当 fragment 膨胀时,抽屉导航启动未知 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56862255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com