按钮不响应onClick方法

时间:2018-11-10 19:27:38

标签: java android button

我正在开发一个Android应用程序,我想通过按一个按钮来增加一个对话框(我已经这样做了),但是一个特定的按钮不起作用,我也不知道为什么。

我的代码:

public class SearchUser extends AppCompatActivity{

private static final String TAG = "SearchUser";

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

    Button b = (Button) findViewById(R.id.btnSearchUser);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder mBuilder = new AlertDialog.Builder(SearchUser.this);
            View mView = getLayoutInflater().inflate(R.layout.searchpopup, null);
            mBuilder.setView(mView);
            AlertDialog dialog = mBuilder.create();
            btnSearch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    });
                }
            });
            dialog.show();
        }
    });
}

布局XML文件:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="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">

<Button
    android:id="@+id/btnUser"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="@string/user"
    mapbox:layout_constraintBottom_toBottomOf="parent"
    mapbox:layout_constraintEnd_toEndOf="@+id/mapView"
    mapbox:layout_constraintHorizontal_bias="0.0"
    mapbox:layout_constraintStart_toStartOf="parent"
    mapbox:layout_constraintTop_toTopOf="@+id/mapView"
    mapbox:layout_constraintVertical_bias="0.0" />

<Button
    android:id="@+id/btnSearchUser"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="@string/search_user"
    mapbox:layout_constraintBottom_toBottomOf="parent"
    mapbox:layout_constraintEnd_toEndOf="@+id/mapView"
    mapbox:layout_constraintHorizontal_bias="0.0"
    mapbox:layout_constraintStart_toStartOf="parent"
    mapbox:layout_constraintTop_toTopOf="@+id/mapView"
    mapbox:layout_constraintVertical_bias="0.129" />

<Button
    android:id="@+id/logoutButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="@string/log_out"
    mapbox:layout_constraintBottom_toBottomOf="parent"
    mapbox:layout_constraintEnd_toEndOf="@+id/mapView"
    mapbox:layout_constraintHorizontal_bias="1.0"
    mapbox:layout_constraintStart_toStartOf="@+id/mapView"
    mapbox:layout_constraintTop_toTopOf="parent"
    mapbox:layout_constraintVertical_bias="0.0" />

<com.mapbox.mapboxsdk.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    mapbox:layout_constraintBottom_toBottomOf="parent"
    mapbox:layout_constraintEnd_toEndOf="parent"
    mapbox:layout_constraintStart_toStartOf="parent"
    mapbox:layout_constraintTop_toTopOf="parent"

    mapbox:mapbox_cameraTargetLat="-34.6131500"
    mapbox:mapbox_cameraTargetLng="-58.3772300"
    mapbox:mapbox_cameraZoom="12"
    mapbox:mapbox_styleUrl="mapbox://styles/mapbox/streets-v10">

</com.mapbox.mapboxsdk.maps.MapView>

<Button
    android:id="@+id/startButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="24dp"
    android:layout_marginEnd="24dp"
    android:layout_marginStart="24dp"
    android:enabled="true"
    android:text="@string/crear_fiesta"
    android:textColor="@color/mapboxGrayLight"
    mapbox:layout_constraintBottom_toBottomOf="parent"
    mapbox:layout_constraintEnd_toEndOf="parent"
    mapbox:layout_constraintStart_toStartOf="parent" />

我尝试过在onClick方法中放入一条祝酒消息,但是什么也没有发生,所以我想它只是没有输入。

有人知道为什么吗?谢谢你的回答:D

2 个答案:

答案 0 :(得分:0)

您要覆盖错误的onCreate(),将其替换为:

protected void onCreate(@Nullable Bundle savedInstanceState)

编辑:您确定应该在类SearchUser中夸大此xml吗?
是否有像xml状态一样的类MainActivity

答案 1 :(得分:0)

btnSearchUser按钮放置在XML中的MapView下方。

您还用MapView配置了match_parent,这不适用于约束布局父级。也将MapView的高度和宽度设置为0dp。