如何修复在弹出窗口中按按钮时出现的错误。

huangapple go评论167阅读模式
英文:

How to fix an error when pressing button in pop-up

问题

I don't know what I am doing wrong, I have a pop up with a button that should take the value of an edit text but when it takes the value and save it in a string it closes, probing to get the text and put a confirm.setVisibility (View.VISIBILITY); but any changes you make to the xml are closed, it is also placed by placing a btnReset.setOnClickListener () on the button; but that only causes it to close as soon as I open the pop up.

This is the xml of the pop up:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:onClick="Cerrar"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="15dp"
        android:paddingTop="10dp"
        android:paddingRight="15dp">

        <ImageButton
            android:id="@+id/botonCerrar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:background="#E6E6E6"
            android:contentDescription="@string/imagen"
            android:onClick="Cerrar"
            app:srcCompat="@drawable/ic_cerrar" />

        <TextView
            android:id="@+id/info_popResetP"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/info_popResetP"
            android:textColor="@color/colorBlack"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/correo_popResetP"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="@string/correo_login"
            android:importantForAutofill="no"
            android:inputType="textPersonName" />

        <Button
            android:id="@+id/botonReset"
            android:layout_width="217dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:background="@drawable/b_morado_base"
            android:onClick="BtnReset"
            android:text="@string/reset_popResetP"
            android:textColor="@color/colorWhite" />

        <TextView
            android:id="@+id/terminar_popResetP"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:foregroundGravity="center"
            android:textColor="@color/colorAccent"
            android:visibility="invisible" />
    </LinearLayout>
</ScrollView>

This is the java code of the pop up:

private TextView confirmar;
private EditText correo;
private Button btnReset;
private String sCorreo = "";
private FirebaseAuth fireAuth;
private ProgressDialog pDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {

    confirmar = (TextView) findViewById(R.id.terminar_popResetP);
    correo = (EditText) findViewById(R.id.correo_popResetP);
    btnReset = (Button) findViewById(R.id.botonReset);
    fireAuth = FirebaseAuth.getInstance();
    pDialog = new ProgressDialog(this);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pop_reset_password);

    DisplayMetrics medidas = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(medidas);

    int ancho = medidas.widthPixels;
    int alto = medidas.heightPixels;

    getWindow().setLayout((int)(ancho*0.85), (int)(alto*0.5));
}

public void Cerrar(View view){
    finish();
}

public void BtnReset(View view){
    //NOTE: SOMETHING ERROR HAPPENS WHEN TAKING THE TEXT FROM THE EDIT TEXT OR WHEN A CHANGE IS MADE TO SOME ELEMENT OF THE XML LIKE CHANGING VISIBILITY.
   confirmar.setText("@string/good_popResetP");
    confirmar.setVisibility(View.VISIBLE);
    if(!sCorreo.isEmpty() || sCorreo.contains("@") || sCorreo.contains(".")){
        confirmar.setVisibility(View.INVISIBLE);
        pDialog.setMessage("Verificando existencia de correo...");
        pDialog.setCanceledOnTouchOutside(false);
        pDialog.show();
        ResetPassword();
    }else {
        confirmar.setText("Ingrese correo electronico");
        confirmar.setVisibility(View.VISIBLE);
    }
}

public void ResetPassword (){
    fireAuth.setLanguageCode("es");
    fireAuth.sendPasswordResetEmail(sCorreo).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {

            if (task.isSuccessful()){
                confirmar.setText("@string/good_popResetP");

            }else {
                confirmar.setText("@string/bad_popResetP");
            }
            pDialog.dismiss();
        }
    });
}
英文:

I don't know what I am doing wrong, I have a pop up with a button that should take the value of an edit text but when it takes the value and save it in a string it closes, probing to get the text and put a confirm.setVisibility (View.VISIBILITY); but any changes you make to the xml are closed, it is also placed by placing a btnReset.setOnClickListener () on the button; but that only causes it to close as soon as I open the pop up.

This is the xml of the pop up:

&lt;ScrollView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:onClick=&quot;Cerrar&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;vertical&quot;
android:paddingLeft=&quot;15dp&quot;
android:paddingTop=&quot;10dp&quot;
android:paddingRight=&quot;15dp&quot;&gt;
&lt;ImageButton
android:id=&quot;@+id/botonCerrar&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;end&quot;
android:background=&quot;#E6E6E6&quot;
android:contentDescription=&quot;@string/imagen&quot;
android:onClick=&quot;Cerrar&quot;
app:srcCompat=&quot;@drawable/ic_cerrar&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/info_popResetP&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;@string/info_popResetP&quot;
android:textColor=&quot;@color/colorBlack&quot;
android:textSize=&quot;18sp&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/correo_popResetP&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;10dp&quot;
android:ems=&quot;10&quot;
android:hint=&quot;@string/correo_login&quot;
android:importantForAutofill=&quot;no&quot;
android:inputType=&quot;textPersonName&quot; /&gt;
&lt;Button
android:id=&quot;@+id/botonReset&quot;
android:layout_width=&quot;217dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center&quot;
android:layout_marginTop=&quot;10dp&quot;
android:background=&quot;@drawable/b_morado_base&quot;
android:onClick=&quot;BtnReset&quot;
android:text=&quot;@string/reset_popResetP&quot;
android:textColor=&quot;@color/colorWhite&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/terminar_popResetP&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:foregroundGravity=&quot;center&quot;
android:textColor=&quot;@color/colorAccent&quot;
android:visibility=&quot;invisible&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;/ScrollView&gt;

This is the java code of the pop up:

private TextView confirmar;
private EditText correo;
private Button btnReset;
private String sCorreo = &quot;&quot;;
private FirebaseAuth fireAuth;
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
confirmar = (TextView) findViewById(R.id.terminar_popResetP);
correo = (EditText) findViewById(R.id.correo_popResetP);
btnReset = (Button) findViewById(R.id.botonReset);
fireAuth = FirebaseAuth.getInstance();
pDialog = new ProgressDialog(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_reset_password);
DisplayMetrics medidas = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(medidas);
int ancho = medidas.widthPixels;
int alto = medidas.heightPixels;
getWindow().setLayout((int)(ancho*0.85), (int)(alto*0.5));
}
public void Cerrar(View view){
finish();
}
public void BtnReset(View view){
//NOTE: SOMETHING ERROR HAPPENS WHEN TAKING THE TEXT FROM THE EDIT TEXT OR WHEN A CHANGE IS MADE TO SOME ELEMENT OF THE XML LIKE CHANGING VISIBILITY.
confirmar.setText(&quot;@string/good_popResetP&quot;);
confirmar.setVisibility(View.VISIBLE);
if(!sCorreo.isEmpty() || sCorreo.contains(&quot;@&quot;) || sCorreo.contains(&quot;.&quot;)){
confirmar.setVisibility(View.INVISIBLE);
pDialog.setMessage(&quot;Verificando existencia de correo...&quot;);
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
ResetPassword();
}else {
confirmar.setText(&quot;Ingrese correo electronico&quot;);
confirmar.setVisibility(View.VISIBLE);
}
}
public void ResetPassword (){
fireAuth.setLanguageCode(&quot;es&quot;);
fireAuth.sendPasswordResetEmail(sCorreo).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
@Override
public void onComplete(@NonNull Task&lt;Void&gt; task) {
if (task.isSuccessful()){
confirmar.setText(&quot;@string/good_popResetP&quot;);
}else {
confirmar.setText(&quot;@string/bad_popResetP&quot;);
}
pDialog.dismiss();
}
});
}

答案1

得分: 0

findViewById(....)只有在将视图绑定到类时才有效。

正确的方法:

@Override
protected void onCreate(Bundle savedInstanceState) {

    fireAuth = FirebaseAuth.getInstance();
    pDialog = new ProgressDialog(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pop_reset_password);
    confirmar = (TextView) findViewById(R.id.terminar_popResetP);
    correo = (EditText) findViewById(R.id.correo_popResetP);
    btnReset = (Button) findViewById(R.id.botonReset);

    DisplayMetrics medidas = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(medidas);

    int ancho = medidas.widthPixels;
    int alto = medidas.heightPixels;

    getWindow().setLayout((int)(ancho*0.85), (int)(alto*0.5));
}
英文:

findViewById(....) is only work when you bind the view to the Class

Correct way:

@Override
protected void onCreate(Bundle savedInstanceState) {
fireAuth = FirebaseAuth.getInstance();
pDialog = new ProgressDialog(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_reset_password);
confirmar = (TextView) findViewById(R.id.terminar_popResetP);
correo = (EditText) findViewById(R.id.correo_popResetP);
btnReset = (Button) findViewById(R.id.botonReset);
DisplayMetrics medidas = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(medidas);
int ancho = medidas.widthPixels;
int alto = medidas.heightPixels;
getWindow().setLayout((int)(ancho*0.85), (int)(alto*0.5));
}

huangapple
  • 本文由 发表于 2020年7月23日 03:21:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63041671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定