【问题标题】:No view found for ID未找到 ID 的视图
【发布时间】:2022-01-06 00:24:28
【问题描述】:

我正在尝试使用 onOptionsItemSelected 实现 Fragment 到 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;
        }
    };

}

【问题讨论】:

  • 你的工具栏R.id.main_toolbar是在fragment布局还是在activity布局??
  • 同时添加崩溃日志
  • 在片段布局中
  • [link] (imgur.com/a/uYAeAjZ) 这是崩溃日志
  • cart_fragment_layout你有这个布局吗?

标签: java android android-fragments


【解决方案1】:

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

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

【讨论】:

  • 感谢您的回复,我刚刚在 nav_graph.xml 中为 CartFragment 提供了一个 id,并在 .replace() 中替换了正确的 id 但仍然没有找到视图
  • 它不会在 nav_graph 中。它将在activity_main.xml 中。这是您的根内容视图。
  • 此外,您的主要活动的 onCreate 也会导致问题 - 您尝试在调用 setContentView 之前调用 findViewById,然后总是返回 null,因为如果没有内容,则不能有匹配的 id查看。
猜你喜欢
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多