2017-12-28 10 views
0

私はフロントページに登録ボタンとサインインボタンがあるときにアンドロイドアプリケーションを作成しています。サインインボタンをクリックするとダイアログボックスが表示されますが、登録ボタンそれは動作しません。そして、私はlogcatを確認しても、それはエラーを投げていません。誰も私がエラーを把握するのを助けることができますか?登録ボタンがクリックされていない

MainActivity.java

package com.example.vishal.uberclone; 

import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.text.TextUtils; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RelativeLayout; 

import com.example.vishal.uberclone.Model.User; 
import com.google.android.gms.tasks.OnFailureListener; 
import com.google.android.gms.tasks.OnSuccessListener; 
import com.google.firebase.auth.AuthResult; 
import com.google.firebase.auth.FirebaseAuth; 
import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 
import com.rengwuxian.materialedittext.MaterialEditText; 

import uk.co.chrisjenx.calligraphy.CalligraphyConfig; 
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper; 

public class MainActivity extends AppCompatActivity{ 

Button btnSignIn,btnRegister; 
RelativeLayout rootLayout; 

FirebaseAuth auth; 
FirebaseDatabase db; 
DatabaseReference users; 

@Override 
protected void attachBaseContext(Context newBase) { 
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() 
      .setDefaultFontPath("fonts/Arkhip_font.ttf") 
      .setFontAttrId(R.attr.fontPath) 
      .build()); 
    setContentView(R.layout.activity_main); 

    auth = FirebaseAuth.getInstance(); 
    db=FirebaseDatabase.getInstance(); 
    users = db.getReference("Users"); 

    btnRegister = (Button)findViewById(R.id.btnRegister); 
    btnSignIn = (Button) findViewById(R.id.btnSignIn); 
    rootLayout =(RelativeLayout) findViewById(R.id.rootLayout); 

    btnRegister.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      showRegisterDialog(); 
     } 
    }); 

    btnSignIn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      showLoginDialog(); 
     } 
    }); 

} 

private void showRegisterDialog() { 
    final AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
    dialog.setTitle("REGISTER"); 
    dialog.setMessage("Please use email to register"); 

    LayoutInflater inflater = LayoutInflater.from(this); 
    View register_layout = inflater.inflate(R.layout.layout_register, null); 

    final MaterialEditText edtEmail = register_layout.findViewById(R.id.edtEmail); 
    final MaterialEditText edtPassword = register_layout.findViewById(R.id.edtPassword); 
    final MaterialEditText edtName = register_layout.findViewById(R.id.edtName); 
    final MaterialEditText edtPhone = register_layout.findViewById(R.id.edtPhone); 

    dialog.setView(register_layout); 

    //Set button 
    dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      dialogInterface.dismiss(); 

      if (TextUtils.isEmpty(edtEmail.getText().toString())) { 
       Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT).show(); 

       return; 
      } 

      if (TextUtils.isEmpty(edtPassword.getText().toString())) { 
       Snackbar.make(rootLayout, "Please enter password", Snackbar.LENGTH_SHORT).show(); 

       return; 
      } 

      if (edtPassword.getText().toString().length() < 6) { 
       Snackbar.make(rootLayout, "Password too short!!!", Snackbar.LENGTH_SHORT).show(); 

       return; 
      } 

      if (TextUtils.isEmpty(edtName.getText().toString())) { 
       Snackbar.make(rootLayout, "Please enter your name", Snackbar.LENGTH_SHORT).show(); 

       return; 
      } 

      if (TextUtils.isEmpty(edtPhone.getText().toString())) { 
       Snackbar.make(rootLayout, "Please enter phone number", Snackbar.LENGTH_SHORT).show(); 

       return; 
      } 

      auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString()) 
        .addOnSuccessListener(new OnSuccessListener<AuthResult>() { 
         @Override 
         public void onSuccess(AuthResult authResult) { 
          //Save user to db 

          User user = new User(); 
          user.setEmail(edtEmail.getText().toString()); 
          user.setName(edtName.getText().toString()); 
          user.setPhone(edtPhone.getText().toString()); 
          user.setPassword(edtPassword.getText().toString()); 

          //Use email to key 
          users.child(user.getEmail()) 
            .setValue(user) 
            .addOnSuccessListener(new OnSuccessListener<Void>() { 
             @Override 
             public void onSuccess(Void aVoid) { 
              Snackbar.make(rootLayout,"Registered successfully",Snackbar.LENGTH_SHORT) 
                .show(); 

             } 
            }) 

            .addOnFailureListener(new OnFailureListener() { 
             @Override 
             public void onFailure(@NonNull Exception e) { 
              Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT) 
                .show(); 
             } 
            }); 



         } 
        }); 






          dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialogInterface, int i) { 
            dialogInterface.dismiss(); 
           } 
          }); 

          dialog.show(); 

         } 
        }); 
     } 


private void showLoginDialog() { 

    final AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
    dialog.setTitle("SIGN IN"); 
    dialog.setMessage("Please use email to sign in"); 

    LayoutInflater inflater = LayoutInflater.from(this); 
    View login_layout = inflater.inflate(R.layout.layout_login,null); 

    final MaterialEditText edtEmail = login_layout.findViewById(R.id.edtEmail); 
    final MaterialEditText edtPassword = login_layout.findViewById(R.id.edtPassword); 

    dialog.setView(login_layout); 

    dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
        dialogInterface.dismiss(); 

        if (TextUtils.isEmpty(edtEmail.getText().toString())) { 
         Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT).show(); 

         return; 
        } 

        if (TextUtils.isEmpty(edtPassword.getText().toString())) { 
         Snackbar.make(rootLayout, "Please enter password", Snackbar.LENGTH_SHORT).show(); 

         return; 
        } 

        if (edtPassword.getText().toString().length() < 6) { 
         Snackbar.make(rootLayout, "Password too short!!!", Snackbar.LENGTH_SHORT).show(); 

         return; 
        } 

        //Login 
        auth.signInWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString()) 
          .addOnSuccessListener(new OnSuccessListener<AuthResult>() { 
           @Override 
           public void onSuccess(AuthResult authResult) { 
            startActivity(new Intent(MainActivity.this,Welcome.class)); 
            finish(); 
           } 
          }).addOnFailureListener(new OnFailureListener() { 
         @Override 
         public void onFailure(@NonNull Exception e) { 
          Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT).show(); 
         } 
        }); 

       } 
      }); 

    dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      dialogInterface.dismiss(); 
     } 
    }); 

      dialog.show(); 

     } 
} 

User.java

package com.example.vishal.uberclone.Model; 

public class User { 

private String email,password,phone,name; 

public User(){ 

} 

public User(String email, String password, String name, String phone){ 
    this.email = email; 
    this.password = password; 
    this.name = name; 
    this.phone = phone; 
} 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

public String getPassword() { 
    return password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 

public String getPhone() { 
    return phone; 
} 

public void setPhone(String phone) { 
    this.phone = phone; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 
} 

Welcome.java

package com.example.vishal.uberclone; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class Welcome extends AppCompatActivity { 

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:id="@+id/rootLayout" 
android:background="@drawable/background" 
tools:context="com.example.vishal.uberclone.MainActivity"> 


<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_marginTop="30dp" 
    android:layout_centerHorizontal="true" 
    android:gravity="center_horizontal"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="U B E R" 
     android:textSize="36sp" 
     android:textAlignment="center" 
     android:textColor="@android:color/white" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:text="PARTNER" 
     android:textAlignment="center" 
     android:textColor="@android:color/white" 
     android:textSize="14sp" /> 



</LinearLayout> 

<TextView 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="20dp" 
    android:text="LOOKING FOR THE RIDER APP?" 
    android:textAlignment="center" 
    android:textColor="@color/bottomText" 
    android:textSize="12sp" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_margin="16dp" 
    android:layout_above="@+id/text"> 

    <Button 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:textColor="@android:color/white" 
     android:background="@drawable/btn_signn_in_background" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="6dp" 
     android:id="@+id/btnSignIn" 
     android:text="SIGN IN"/> 

    <Button 
     android:id="@+id/btnRegister" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="6dp" 
     android:layout_weight="1" 
     android:background="@drawable/btn_register_background" 
     android:text="REGISTER" 
     android:textColor="@color/btnRegister" /> 

</LinearLayout> 

</RelativeLayout> 
あなたはpositiveButtonから OnClickListener内部 dialog.show();を呼び出している

layout_login.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
app:cardElevation="10dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_margin="20dp"> 

    <com.rengwuxian.materialedittext.MaterialEditText 
     android:id="@+id/edtEmail" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:hint="Email" 
     android:inputType="textEmailAddress" 
     android:textColor="@color/colorPrimary" 
     android:textColorHint="@color/colorPrimary" 
     android:textSize="20sp" 
     app:met_baseColor="@color/colorPrimary" 
     app:met_singleLineEllipsis="true" 
     app:met_floatingLabel="highlight" 
     app:met_primaryColor="@color/colorPrimary"/> 

    <com.rengwuxian.materialedittext.MaterialEditText 
     android:id="@+id/edtPassword" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:hint="Password" 
     android:inputType="textPassword" 
     android:textColor="@color/colorPrimary" 
     android:textColorHint="@color/colorPrimary" 
     android:textSize="20sp" 
     app:met_baseColor="@color/colorPrimary" 
     app:met_singleLineEllipsis="true" 
     app:met_floatingLabel="highlight" 
     app:met_primaryColor="@color/colorPrimary"/> 

</LinearLayout> 

</android.support.v7.widget.CardView> 

layout_register.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
app:cardElevation="10dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_margin="20dp"> 

    <com.rengwuxian.materialedittext.MaterialEditText 
     android:id="@+id/edtEmail" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:hint="Email" 
     android:inputType="textEmailAddress" 
     android:textColor="@color/colorPrimary" 
     android:textColorHint="@color/colorPrimary" 
     android:textSize="20sp" 

     app:met_baseColor="@color/colorPrimary" 
     app:met_singleLineEllipsis="true" 
     app:met_floatingLabel="highlight" 
     app:met_primaryColor="@color/colorPrimary"/> 

    <com.rengwuxian.materialedittext.MaterialEditText 
     android:id="@+id/edtPassword" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:hint="Password" 
     android:inputType="textPassword" 
     android:textColor="@color/colorPrimary" 
     android:textColorHint="@color/colorPrimary" 
     android:textSize="20sp" 

     app:met_baseColor="@color/colorPrimary" 
     app:met_singleLineEllipsis="true" 
     app:met_floatingLabel="highlight" 
     app:met_primaryColor="@color/colorPrimary"/> 

    <com.rengwuxian.materialedittext.MaterialEditText 
     android:id="@+id/edtName" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:hint="Name" 
     android:inputType="text" 
     android:textColor="@color/colorPrimary" 
     android:textColorHint="@color/colorPrimary" 
     android:textSize="20sp" 

     app:met_baseColor="@color/colorPrimary" 
     app:met_singleLineEllipsis="true" 
     app:met_floatingLabel="highlight" 
     app:met_primaryColor="@color/colorPrimary"/> 

    <com.rengwuxian.materialedittext.MaterialEditText 
     android:id="@+id/edtPhone" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:hint="Phone" 
     android:inputType="phone" 
     android:textColor="@color/colorPrimary" 
     android:textColorHint="@color/colorPrimary" 
     android:textSize="20sp" 

     app:met_baseColor="@color/colorPrimary" 
     app:met_singleLineEllipsis="true" 
     app:met_floatingLabel="highlight" 
     app:met_primaryColor="@color/colorPrimary"/> 

</LinearLayout> 

</android.support.v7.widget.CardView> 

答えて

0

。あなたはそれを外に呼び出さなければなりません:

private void showRegisterDialog() { 
    final AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
    dialog.setTitle("REGISTER"); 
    dialog.setMessage("Please use email to register"); 

    LayoutInflater inflater = LayoutInflater.from(this); 
    View register_layout = inflater.inflate(R.layout.layout_register, null); 

    final MaterialEditText edtEmail = register_layout.findViewById(R.id.edtEmail); 
    final MaterialEditText edtPassword = register_layout.findViewById(R.id.edtPassword); 
    final MaterialEditText edtName = register_layout.findViewById(R.id.edtName); 
    final MaterialEditText edtPhone = register_layout.findViewById(R.id.edtPhone); 

    dialog.setView(register_layout); 

    //Set button 
    dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      dialogInterface.dismiss(); 

      if (TextUtils.isEmpty(edtEmail.getText().toString())) { 
       Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT).show(); 

       return; 
      } 

      if (TextUtils.isEmpty(edtPassword.getText().toString())) { 
       Snackbar.make(rootLayout, "Please enter password", Snackbar.LENGTH_SHORT).show(); 

       return; 
      } 

      if (edtPassword.getText().toString().length() < 6) { 
       Snackbar.make(rootLayout, "Password too short!!!", Snackbar.LENGTH_SHORT).show(); 

       return; 
      } 

      if (TextUtils.isEmpty(edtName.getText().toString())) { 
       Snackbar.make(rootLayout, "Please enter your name", Snackbar.LENGTH_SHORT).show(); 

       return; 
      } 

      if (TextUtils.isEmpty(edtPhone.getText().toString())) { 
       Snackbar.make(rootLayout, "Please enter phone number", Snackbar.LENGTH_SHORT).show(); 

       return; 
      } 

      auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString()) 
        .addOnSuccessListener(new OnSuccessListener<AuthResult>() { 
         @Override 
         public void onSuccess(AuthResult authResult) { 
          //Save user to db 

          User user = new User(); 
          user.setEmail(edtEmail.getText().toString()); 
          user.setName(edtName.getText().toString()); 
          user.setPhone(edtPhone.getText().toString()); 
          user.setPassword(edtPassword.getText().toString()); 

          //Use email to key 
          users.child(user.getEmail()) 
            .setValue(user) 
            .addOnSuccessListener(new OnSuccessListener<Void>() { 
             @Override 
             public void onSuccess(Void aVoid) { 
              Snackbar.make(rootLayout,"Registered successfully",Snackbar.LENGTH_SHORT) 
                .show(); 

             } 
            }) 

            .addOnFailureListener(new OnFailureListener() { 
             @Override 
             public void onFailure(@NonNull Exception e) { 
              Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT) 
                .show(); 
             } 
            }); 



         } 
        }); 
     } 
    }); 

    dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      dialogInterface.dismiss(); 
     } 
    }); 

    dialog.show(); 
} 
+0

ありがとうございました...それは働いた... – Vishal

+0

問題ない、嬉しいです! –

+0

このエラーも発生しています--- java.lang.NullPointerException:仮想メソッド 'com.google.android.gms.tasks.Task com.google.android.gms.common.api.GoogleApiを呼び出そうとしています.zzb(com.google.android.gms.common.api.internal.zzdd) 'nullオブジェクト参照 – Vishal

関連する問題