找不到 ID 的视图
No view found for ID
我正在尝试通过 onOptionsItemSelected
实现分片到分片交易,但我一直收到
java.lang.IllegalArgumentException: No view found for 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,然后弹出已经存在的内容并用您的片段替换它。
我正在尝试通过 onOptionsItemSelected
实现分片到分片交易,但我一直收到
java.lang.IllegalArgumentException: No view found for 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,然后弹出已经存在的内容并用您的片段替换它。