LoginSignup
kaedekaedekaede0703
@kaedekaedekaede0703

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

AndroidStudioでsetOnClickListenerが反映されない。

Android Studioでjavaを使って書いてます。
ボタンにsetOnClickListenerを用いてボタンを押したときにLogを出力させようとしたのですが、出力されません。
hasOnClickListenersでリスナーを確認したところtrueと出力されたのでリスナーの追加は成功していると思います。またsetOnClickListenerをコメントアウトしたところ、hasOnClickListenersもfalseになりました。

原因が分からず困っています。助言いただけますとありがたいです。

以下javaのコードです。


package com.example.obj2_1226;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = findViewById(R.id.notification);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("test4", "TEST4");
            }
        });
    }
}


以下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:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="bottom">
    
        <Button
            android:id="@+id/notification"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="通知" />
    
        <Button
            android:id="@+id/dialog_button_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="ダイアログ1" />
    
        <Button
            android:id="@+id/dialog_button_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="ダイアログ2" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
0

No Answers yet.

Your answer might help someone💌