简体   繁体   中英

onClickListener() not working on FloatingActionButton and Toolbar Icon

the onClickListener of my FloatingActionButton and Toolbar Icon are not working. Here is the code:

    floatingActionButton = findViewById(R.id.pausebutton);
    floatingActionButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            EventHandlerClass.releaseMediaPlayer();
        }
    });

    search_ico=findViewById(R.id.search_ico);
    search_ico.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this,searchActivity.class));
        }
    });

It is weird because the "new OnClickListener()" is greyed out like this: 变灰

My clicks are going through the FloatingActionButton, here is the code:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center|bottom"
    android:layout_marginBottom="60dp"
    android:src="@drawable/paused"
    app:fabSize="normal"
    app:backgroundTint="#38464E"
    app:elevation="20dp"
    android:id="@+id/pausebutton"/>

I have absolutely no Idea what is wrong, looks right to me. Does anyone have some Ideas what I could try?

I fixed it my own, the problem was that I had this line after the onClickListeners:

setContentView(R.layout.activity_main);

But this line hast to be right at the top of the OnCreate

You can click on it and press option + Enter . The IDE will tell you it can be replace with lambda 在此处输入图片说明

it can make the code more clear

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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