Whenever I run the app, it closes itself

Asked

Viewed 148 times

-2

Good afternoon, I’m having a problem and can’t solve it. Every time I try to test the app on the phone it closes alone:

Logcat: 2020-05-09 16:02:58.397 30691-30691/com.leandrel.Uri E/Androidruntime: FATAL EXCEPTION: main Process: com.leandrel.Uri, PID: 30691 java.lang.Illegalstateexception: Could not execute method for android:onClick at androidx.appcompat.app.Appcompatviewinflater$Declaredonclicklistener.onClick(Appcompatviewinflater.java:390) at android.view.View.performClick(View.java:6608) at android.view.View.performClickInternal(View.java:6585) at android.view.View.access$3100(View.java:785) at android.view.View$Performclick.run(View.java:25921) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:201) at android.app.Activitythread.main(Activitythread.java:6810) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Runtimeinit$Methodandargscaller.run(Runtimeinit.java:547) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:873) Caused by: java.lang.reflect.Invocationtargetexception at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompatcompat.app.Appcompatviewinflater$Declaredonclicklistener.onClick(Appcompatviewinflater.java:385) at android.view.View.performClick(View.java:6608)  at android.view.View.performClickInternal(View.java:6585)  at android.view.View.access$3100(View.java:785)  at android.view.View$Performclick.run(View.java:25921)  at android.os.Handler.handleCallback(Handler.java:873)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:201)  at android.app.Activitythread.main(Activitythread.java:6810)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.Internal.os.Runtimeinit$Methodandargscaller.run(Runtimeinit.java:547)  at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:873)  Caused by: java.lang.Numberformatexception: For input string: "androidx.appcompatcompat.widget.Appcompatedittext{d676bdc VFED. .CL. .F..... 552,916-901,1092 #7f070037 app:id/edtNmr}" at java.lang.Integer.parseint(Integer.java:615) at java.lang.Integer.parseint(Integer.java:650) at com.leandrel.Uri.MainActivity.verificar(Mainactivity.java:23) at java.lang.reflect.Method.invoke(Native Method)  at androidx.appcompatcompat.app.Appcompatviewinflater$Declaredonclicklistener.onClick(Appcompatviewinflater.java:385)  at android.view.View.performClick(View.java:6608)  at android.view.View.performClickInternal(View.java:6585)  at android.view.View.access$3100(View.java:785)  at android.view.View$Performclick.run(View.java:25921)  at android.os.Handler.handleCallback(Handler.java:873)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:201)  at android.app.Activitythread.main(Activitythread.java:6810)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.Internal.os.Runtimeinit$Methodandargscaller.run(Runtimeinit.java:547)  at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:873)  2020-05-09 16:02:58.476 30691-30691/com.leandrel.Uri I/Process: Sending Signal. PID: 30691 SIG: 9

The code in Java:

 package com.leandrel.uri;

    import androidx.appcompat.app.AppCompatActivity;

    import android.os.Bundle;
    import android.view.View;
    import android.view.WindowManager;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            EditText nmr = findViewById(R.id.edtNmr);
        }

        public void verificar(View view) {
            EditText nmr = findViewById(R.id.edtNmr);
            String conv = String.valueOf(nmr);
            int inteiro = Integer.parseInt(conv);
            int i = 0, soma = 0;
            TextView result = findViewById(R.id.txvResult);
            for(i=1;i<inteiro;i++){
                if(i%inteiro==0){
                    soma += i;
                }
            }
            if(soma==inteiro){

                result.setText("O número "+inteiro+" é perfeito");
            }else{

                result.setText("O número "+inteiro+" não é perfeito");
            }
        }
    }

1 answer

0


Try to improve the structure of your code by making it readable/easy for you and others in the future.

From what I’ve seen, you call this function verify() from the click on a certain button? And it receives as parameter a view. Also make use of Try-Catch or create your own exceptions to make it easier to read the error in logcat

public class MainActivity extends AppCompatActivity {
//region Variaveis das suas views
private EditText nmr;
private TextView result;
private Button seuBotaoDeAcao;
//endregion

//region Strings
private String TAG = "MainActivity";
//endregion

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

    //region Referência das views
    nmr                 = findViewById(R.id.edtNmr);
    result              = findViewById(R.id.txvResult);
    seuBotaoDeAcao      = findViewById(R.id.seu_botao_acao);
    //endregion

    //region Ação do seuBotaoDeAcao
    seuBotaoDeAcao.setOnClickListener(v -> verificar(nmr.getText().toString().replaceAll(" ", "")));
    //endregion

}

private void verificar(String conv) {
    try{
        int inteiro = Integer.parseInt(conv);
        int soma = 0;

        for(int i = 1; i < inteiro; i++){
            if(i%inteiro == 0) soma += i;
        }

        String result = soma == inteiro ? "é perfeito" : "não é perfeito";
        result.setText("O número "+inteiro+" "+result);
    }catch(Exception ex){
        Log.e(TAG,"Erro na função verificar: "+ex.getMessage());
    }
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.