Android 片段按钮内的操作侦听器不工作

Android 片段按钮内的操作侦听器不工作,android,android-layout,android-fragments,layout-inflater,Android,Android Layout,Android Fragments,Layout Inflater,我有一个底部选项卡式活动,包括主页、仪表板和通知选项卡。现在默认情况下,android studio以片段的形式提供这些选项卡。我在fragment_home.xml中有一个按钮,但我的当前视图设置为activity_main.xml。fragment类中的button.onClick侦听器似乎不起作用。如何解决这个问题 主类 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(saved

我有一个底部选项卡式活动,包括主页、仪表板和通知选项卡。现在默认情况下,android studio以片段的形式提供这些选项卡。我在fragment_home.xml中有一个按钮,但我的当前视图设置为activity_main.xml。fragment类中的button.onClick侦听器似乎不起作用。如何解决这个问题

主类

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BottomNavigationView navView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications).build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
    NavigationUI.setupWithNavController(navView, navController);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowCustomEnabled(true);
    View cView = getLayoutInflater().inflate(R.layout.cutom_toolbar, null);
    actionBar.setCustomView(cView);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

boolean doubleBackToExitPressedOnce = false;

@Override
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce=false;
        }
    }, 2000);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId()){
        case R.id.menu1:
            Toast.makeText(this, "Clicked Menu 1", Toast.LENGTH_SHORT).show();
            break;
        case R.id.menu2:
            Toast.makeText(this, "Clicked Menu 2", Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
    }
    return super.onOptionsItemSelected(item);
}
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:grid="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:background="@drawable/image"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize"
    grid:alignmentMode="alignBounds"
    grid:columnCount="2"
    grid:rowOrderPreserved="false"
    grid:useDefaultMargins="true">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="@drawable/image1"
        app:itemIconTint="@color/color"
        app:itemTextColor="@color/color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

不清楚。如果我当时是对的, 在片段中声明一个全局可验证性

public Button button;
将其初始化为onViewCreated()

在你的主要活动中

if(fragment.button != null){
   //fire action
}

fragment_home.xml
中声明的视图元素需要在片段代码中交互,而不是在活动中交互

使用NavHost意味着您的大部分代码将在单个片段中,而不是在承载片段的活动中

如有必要,您可以使用接口在两者之间进行通信。
您还可以使用共享的ViewModel进行通信—让片段中的onClick侦听器在订阅MainActivity的ViewModel中设置一个可观察的对象。

欢迎使用Stack Overflow。如果你能发布一些相关的代码来说明你的问题,我们可以更好地帮助你。很抱歉评论太晚。我现在已经更新了代码。这是向后的。在片段中定义接口,但让活动实现它。@+id/imageView2,此视图位于在HomeFragment中膨胀的fragment_home.xml布局中。如果需要访问活动中的按钮,可以设置回调。您可以使用主活动实现的接口,当在片段内部按下按钮时,可以通过接口发送数据?但我认为只有片段类本身中的Actionlistener才能实现这一点。你的意思是说我应该在该接口中用button.onClick代码定义一个接口X,然后从我的主类实现它吗?我认为最好的方法是在主片段代码本身内部处理onClick。如果您必须在活动和片段之间进行通信,有多种方法。一种流行的新方法是使用共享视图模型。是的,我只想在片段中的按钮上安装一个动作监听器。我已经用带有动作监听器的片段类更新了代码。但是这个动作不起作用,而且Android studio说从未使用过类fragment1?
public Button button;
if(fragment.button != null){
   //fire action
}