J'essaie de lire les données d'une boîte de dialogue (page d'inscription) ci-dessous: entrez la description de l'image ici

Le code est:

private void loginAndSignupDialog() {
    final Dialog dialog = new Dialog(SelectServiceActivity.this);
    dialog.setContentView(R.layout.dialog_login);
    dialog.setTitle("Login");
    dialog.findViewById(R.id.login_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String username = ((EditText) dialog.findViewById(R.id.username)).getText().toString();
            final String password = ((EditText) dialog.findViewById(R.id.password)).getText().toString();
            validateUser(username, password, dialog);
        }
    });
    dialog.findViewById(R.id.to_signup_page).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //final Dialog dialog = new Dialog(SelectServiceActivity.this);
            dialog.setContentView(R.layout.dialog_signup);
            dialog.setTitle("Signup");
            dialog.findViewById(R.id.signup).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final String email = ((EditText) dialog.findViewById(R.id.email)).getText().toString();
                    final String password = ((EditText) dialog.findViewById(R.id.password)).getText().toString();
                    final String confirmPassword = ((EditText) dialog.findViewById(R.id.confirm_password)).getText().toString();
                    final String mobileNo = ((EditText) dialog.findViewById(R.id.mobile_number)).getText().toString();
                    if (password.equals(confirmPassword)) {
                        registerUser(email, password, mobileNo);
                    } else {
                        Toast.makeText(SelectServiceActivity.this, "Two passwords are not same.", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    });
    dialog.show();
}

Je reçois une "exception de pointeur nul" à ces lignes:

final String email = ((EditText) dialog.findViewById(R.id.email)).getText().toString();
                        final String password = ((EditText) dialog.findViewById(R.id.password)).getText().toString();
                        final String confirmPassword = ((EditText) dialog.findViewById(R.id.confirm_password)).getText().toString();
                        final String mobileNo = ((EditText) dialog.findViewById(R.id.mobile_number)).getText().toString();

Le scénario est que je passe de la boîte de dialogue de connexion à la page de dialogue d'inscription en cliquant sur le texte INSCRIPTION sur la page de connexion ci-dessous:

enter image description here

J'obtiens des données sur la page de connexion sans erreur mais avec une exception de pointeur nul sur les attributs de la page d'inscription.

XML de la boîte de dialogue d'inscription

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat 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"
    android:layout_gravity="center"
    android:layout_margin="20sp"
    android:gravity="center"
    android:orientation="vertical"
    android:overScrollMode="ifContentScrolls"
    android:scrollbars="none"
    tools:context="in.co.servicedeal.app.SignupActivity">
    <android.support.v7.widget.AppCompatImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/sd_logo" />
    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/email"
        android:inputType="textEmailAddress" />
    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/signup_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/password"
        android:inputType="textPassword" />
    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/confirm_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/confirmPassword"
        android:inputType="textPassword" />
    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/mobile_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/mobileNumber"
        android:inputType="phone" />
    <android.support.v7.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.AppCompatCheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            app:buttonTint="@color/colorPrimary" />
        <android.support.v7.widget.AppCompatTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/serviceAggrement" />
    </android.support.v7.widget.LinearLayoutCompat>
    <android.support.v7.widget.AppCompatButton
        android:id="@+id/signup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10sp"
        android:background="@color/colorPrimary"
        android:elevation="@dimen/card_elevation"
        android:text="@string/signUp"
        android:textColor="@android:color/white"
        android:textSize="20sp" />
</android.support.v7.widget.LinearLayoutCompat>

Boîte de dialogue XML de connexion

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_margin="20sp"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="in.co.servicedeal.app.LoginActivity">
    <android.support.v7.widget.AppCompatImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/sd_logo" />
    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/userName" />
    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/password"
        android:inputType="textPassword" />
    <android.support.v7.widget.AppCompatButton
        android:id="@+id/login_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25sp"
        android:background="@color/colorPrimary"
        android:text="@string/login"
        android:textColor="@android:color/white"
        android:textSize="20sp" />
    <android.support.v7.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20sp"
        android:layout_marginTop="10sp"
        android:gravity="center">
        <android.support.v7.widget.AppCompatTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/dontHaveAcct" />
        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/to_signup_page"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5sp"
            android:text="@string/signUp"
            android:textStyle="bold" />
    </android.support.v7.widget.LinearLayoutCompat>
</android.support.v7.widget.LinearLayoutCompat>

Veuillez suggérer!

1
Shamim Ahmad 3 août 2017 à 10:54

2 réponses

Votre email, mot de passe, confirmPassword, les champs mobilno sont nuls.

Essaye ça:

final String email = ((EditText)v dialog.findViewById(R.id.email)).getText().toString();
final String password = ((EditText)v dialog.findViewById(R.id.password)).getText().toString();

final String confirmPassword = ((EditText)v dialog.findViewById(R.id.confirm_password)).getText().toString();

final String mobileNo = ((EditText)v dialog.findViewById(R.id.mobile_number)).getText().toString();
0
Ugur Tufekci 3 août 2017 à 08:06

Essayez de créer votre interface DialogOnClickListener et mettez-y rempli Bundle

0
Olena Y 3 août 2017 à 08:09