2016-11-17 113 views
0

我試圖做我的自定義對話框控件空引用,在自定義對話框的Android

所以我做了佈局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <EditText 
     android:id="@+id/dialog_newpassord" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="24dp" 
     android:layout_marginLeft="24dp" 
     android:layout_marginRight="24dp" 
     android:layout_marginStart="24dp" 
     android:layout_marginTop="24dp" 
     android:ems="10" 
     android:hint="@string/activity_login_field_password_hint" 
     android:inputType="textPassword" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/dialog_title"/> 

    <EditText 
     android:id="@+id/dialog_newpassord2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="24dp" 
     android:layout_marginLeft="24dp" 
     android:layout_marginRight="24dp" 
     android:layout_marginStart="24dp" 
     android:layout_marginTop="16dp" 
     android:ems="10" 
     android:hint="@string/dialog_newPassword" 
     android:inputType="textPassword" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/dialog_newpassord"/> 

    <TextView 
     android:id="@+id/dialog_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="24dp" 
     android:layout_marginStart="24dp" 
     android:layout_marginTop="24dp" 
     android:text="TextView" 
     android:textAppearance="@style/TextAppearance.AppCompat.Medium" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent"/> 

    <Button 
     android:id="@+id/dialog_button_ko" 
     android:layout_width="wrap_content" 
     android:layout_height="36dp" 
     android:layout_marginBottom="16dp" 
     android:layout_marginEnd="16dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginTop="16dp" 
     android:text="@android:string/cancel" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/dialog_button_ok" 
     app:layout_constraintTop_toBottomOf="@+id/dialog_newpassord2"/> 

    <Button 
     android:id="@+id/dialog_button_ok" 
     android:layout_width="wrap_content" 
     android:layout_height="36dp" 
     android:layout_marginBottom="16dp" 
     android:layout_marginEnd="24dp" 
     android:layout_marginRight="24dp" 
     android:layout_marginTop="16dp" 
     android:text="@string/ok" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/dialog_newpassord2"/> 

    <TextView 
     android:id="@+id/dialog_alert" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="TextView" 
     android:visibility="gone" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/dialog_newpassord2"/> 
</android.support.constraint.ConstraintLayout> 

然後在活動中我有:

private void newPasswordDialog(){ 
     Dialog dialog = new Dialog(ctx); 
     dialog.setContentView(R.layout.dialog_login); 
     dialog.setTitle("New password"); 

     final EditText password = (EditText) dialog.findViewById(R.id.dialog_newpassord); 
     final EditText password2 = (EditText) dialog.findViewById(R.id.dialog_newpassord2); 
     Button cancel = (Button) dialog.findViewById(R.id.dialog_button_ko); 
     Button accept = (Button) dialog.findViewById(R.id.dialog_button_ok); 

     cancel.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

      } 
     }); 
     accept.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if(isEmptyEditText(password) || isEmptyEditText(password2)) 
       { 
        //Lanzar visibility y meter texto 
       } 
       else 
       { 
        //comprobar que son iguales los passwords 
       } 
      } 
     }); 

    } 

但是當我啓動應用程序並調用newPasswordDialog時,所有對小部件的引用都設置爲null,並且很明顯當到達cancel.onClic{..}時它崩潰

如何避免這種空引用?

+0

問題在於你的ctx。你在哪裏調用這個Dialog.Activity或Adapter或Fragment? –

+0

從活動調用。 'this.ctx = this;' – Shudy

+0

請嘗試以下回答 –

回答

0

試試這種方法。

private void newPasswordDialog(){ 
    AlertDialog.Builder dialog = new 
    AlertDialog.Builder(yourClass.this);//Activity.this 
    View v=getLayoutInflator.inflate(R.layout.dialog_login,null); 
    dialog.setView(v); 
    //dialog.setContentView(R.layout.dialog_login); 
    dialog.setTitle("New password"); 

    final EditText password = (EditText) v.findViewById(R.id.dialog_newpassord); 
    final EditText password2 = (EditText) v.findViewById(R.id.dialog_newpassord2); 
    Button cancel = (Button) v.findViewById(R.id.dialog_button_ko); 
    Button accept = (Button) v.findViewById(R.id.dialog_button_ok); 

    cancel.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

     } 
    }); 
    accept.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if(isEmptyEditText(password) || isEmptyEditText(password2)) 
      { 
       //Lanzar visibility y meter texto 
      } 
      else 
      { 
       //comprobar que son iguales los passwords 
      } 
     } 
    }); 
    AlertDialog d=dialog.create(); 
    d.show(); 

    } 
+0

已更改,但仍在窗口小部件上返回null。我改爲:'dialog.findViewById(...)'到'v.findViewById(...)',因爲AS是在對話框中不可能的「警告」。即使是這種變化,也沒有返回參考。 – Shudy

+1

'getLayoutInflator.inflate'甚至有效嗎? oO 確保你正在給正確的佈局文件充氣@Shudy –

+1

對不起,我不會改變那個@Shudy –