繁体   English   中英

未找到 ID 的视图

[英]No view found for ID

我正在尝试使用 onOptionsItemSelected 实现 Fragment to Fragment 事务,但我不断收到

java.lang.IllegalArgumentException:未找到 id 的视图

我还阅读了这个帖子: Android Fragment no view found for ID? 并对我的代码应用了一些答案,但这并不能解决我的问题

这是我的 HomeFragment.java

    private Toolbar toolbar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //Set Toolbar for Fragment
        View v = inflater.inflate(R.layout.fragment_home, container, false);
        toolbar = (Toolbar) v.findViewById(R.id.main_toolbar);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        setHasOptionsMenu(true);
        return v;
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.toolbar_menu,menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        fm.beginTransaction().replace(R.id.cart_fragment_layout,new CartFragment())
                .commitNow();
        return super.onOptionsItemSelected(item);
    }
}

我的 fragment_cart.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/cart_fragment_layout"
    tools:context="com.example.fragment.CartFragment"
    android:background="@color/black">

    <!-- TODO: Update blank fragment layout -->
    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/cart_ll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/cart_rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </androidx.recyclerview.widget.RecyclerView>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/white"
        android:textSize="50dp"
        android:text="Cart Fragment" />
    </LinearLayout>

</FrameLayout>

和我的 CartFragment.java

public class CartFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_cart, container, false);
        return v;
    }
}

我的 MainActivity.java 以防万一,我将 HomeFragment 设置为默认值:

public class MainActivity extends AppCompatActivity {
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        toolbar = findViewById(R.id.main_toolbar);
        setSupportActionBar(toolbar);
        setContentView(R.layout.activity_main);
        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);

        bottomNav.setOnNavigationItemSelectedListener(navListener);
        getSupportFragmentManager().beginTransaction().replace(R.id.frament_container,
                new HomeFragment()).commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment selectedFragment = null;

            switch (item.getItemId()){
                case R.id.nav_home:
                    selectedFragment = new HomeFragment();
                    break;
                case R.id.nav_menu:
                    selectedFragment = new MenuFragment();
                    break;
                case R.id.nav_user:
                    selectedFragment = new UserFragment();
                    break;
                case R.id.nav_more:
                    selectedFragment = new MoreFragment();
                    break;
            }
            getSupportFragmentManager().beginTransaction().replace(R.id.frament_container,
                    selectedFragment).commit();
            return true;
        }
    };

}

您在片段事务中使用的 id 不是片段中视图的视图 id。 它是您将片段弹出到的布局中片段存根视图的 ID。 所以你传入了错误的id。 我不能告诉你什么是正确的,因为你没有给我们你的主要布局的 xml。

基本上,片段事务查找您在上下文根布局中传递的 id,然后弹出已经存在的内容并将其替换为您的片段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM