Кнопка не отвечает на метод onClick - PullRequest
0 голосов
/ 10 ноября 2018

Я занимаюсь разработкой приложения для 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 голосов
/ 10 ноября 2018

Поместите кнопку btnSearchUser под MapView в XML.

Вы также настроили MapView с match_parent, что неприменимо для родительского макета ограничения. Установите высоту и ширину от MapView до 0dp.

0 голосов
/ 10 ноября 2018

Вы переопределяете неправильный onCreate(), замените его на:

protected void onCreate(@Nullable Bundle savedInstanceState)

Редактировать : вы уверены, что этот xml должен быть раздут в классе SearchUser?
У вас есть класс MainActivity, например, xml-состояния?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...