Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.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
Java 我正在尝试从OnOptions ItemSelected方法打开一个片段_Java_Android_Android Layout_Android Fragments - Fatal编程技术网

Java 我正在尝试从OnOptions ItemSelected方法打开一个片段

Java 我正在尝试从OnOptions ItemSelected方法打开一个片段,java,android,android-layout,android-fragments,Java,Android,Android Layout,Android Fragments,我试图调用某个活动的片段。但是不能。这就是我试过的 @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { if (item.getItemId()==R.id.action_search){ SearchFragment searchFragment=new SearchFragment(); getSupportFragmentManager().beginTra

我试图调用某个活动的片段。但是不能。这就是我试过的

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getItemId()==R.id.action_search){
        SearchFragment searchFragment=new SearchFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.container,searchFragment,null).commit();
    }
    return true;
}
和我的主活动

<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
tools:context=".Controllers.MainActivity">

如果我做错了什么,请纠正我

您可以使用Framlayout而不是线性布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">

如果您想在
main活动中使用其他组件,请为此创建一个片段。
并替换它

activity\u main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/frameLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

    ...

</LinearLayout>
MainFragment.java

public class MainFragment extends Fragment {

  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup 
     container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_main, container, false);

      //...

      return view;
  }
}

在MainActivity中?在MainActivity xmlIf中使用FrameLayout后崩溃日志,那么视图组呢。使用线性布局进行排列
<RelativeLayout 
    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"
    tools:context=".SearchFragment">

      <androidx.recyclerview.widget.RecyclerView
       android:id="@+id/recyclerViewSearchFrag"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>

</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

    ...

</LinearLayout>
public class MainFragment extends Fragment {

  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup 
     container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_main, container, false);

      //...

      return view;
  }
}