Unable to start activity ComponentInfo

0

The app does not work when deploying. It "crashes" straight. Follow the code and the error:
ActMain.java

package desenvmoveluss.com.br.trabalho01;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
import android.content.*;

public class ActMain extends AppCompatActivity {
//implements View.OnClickListener
    private EditText edtA;
    private EditText edtB;
    private EditText edtC;
    private Button btnOK;
    public String resultado;

    int a;
    int b;
    int c;

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

        a=0;
        b=0;
        c=0;

        edtA = (EditText)findViewById(R.id.edtA);
        edtB = (EditText)findViewById(R.id.edtB);
        edtC = (EditText)findViewById(R.id.edtC);
        btnOK = (Button)findViewById(R.id.btnOk);

        a = Integer.parseInt(edtA.getText().toString());
        b = Integer.parseInt(edtB.getText().toString());
        c = Integer.parseInt(edtC.getText().toString());


        if ((a < b + c) || (b < a + c) || (c < a+b)){

            //triangulo
            if((a==b) && (a==c) && (c==b)){
                resultado = "Triangulo Equilatero";

            }
            if((a==b) || (a==c) || (c==b)){
                resultado = "Triangulo Isosceles";
            }
            else{
                resultado = "Triangulo Escaleno";
            }
        }
        else{
            resultado = "Não é um triangulo";

        }


    }

    public void OnClick(View v){
        Intent it = new Intent(this, ActSegundaTela.class);
        it.putExtra("RESULTADO", resultado.toString());
        startActivity(it);

    }
}

ActSegundaTela.java

package desenvmoveluss.com.br.trabalho01;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;

public class ActSegundaTela extends AppCompatActivity {

    private TextView txtResultado;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_segunda_tela);

        txtResultado = (TextView)findViewById(R.id.txtResultado);

        Intent intent = getIntent();
        String resultado = intent.getExtras().getString("RESULTADO");
       //        String valor = bundle.getString("RESULTADO");

        txtResultado.setText(resultado);

    }
}

Error

09-23 18:05:57.920 8864-8864/desenvmoveluss.com.br.trabalho01 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: desenvmoveluss.com.br.trabalho01, PID: 8864
    java.lang.RuntimeException: Unable to start activity ComponentInfo{desenvmoveluss.com.br.trabalho01/desenvmoveluss.com.br.trabalho01.ActMain}: java.lang.NumberFormatException: Invalid int: ""
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
        at android.app.ActivityThread.access$900(ActivityThread.java:157)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5527)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
     Caused by: java.lang.NumberFormatException: Invalid int: ""
        at java.lang.Integer.invalidInt(Integer.java:138)
        at java.lang.Integer.parseInt(Integer.java:358)
        at java.lang.Integer.parseInt(Integer.java:334)
        at desenvmoveluss.com.br.trabalho01.ActMain.onCreate(ActMain.java:35)
        at android.app.Activity.performCreate(Activity.java:6272)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
        at android.app.ActivityThread.access$900(ActivityThread.java:157) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5527) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
    
asked by anonymous 23.09.2016 / 23:15

3 answers

3

You are converting an empty string ( "" ) to an integer.

a = Integer.parseInt(edtA.getText().toString());

This happens because nothing has yet been entered in EditText .

I suggest you do the following:

Move the code that calculates to a new method:

private String calcular(){

    int a = Integer.parseInt(edtA.getText().toString());
    int b = Integer.parseInt(edtB.getText().toString());
    int c = Integer.parseInt(edtC.getText().toString());


    if ((a < b + c) || (b < a + c) || (c < a+b)){

        //triangulo
        if((a==b) && (a==c) && (c==b)){
            resultado = "Triangulo Equilatero";

        }
        if((a==b) || (a==c) || (c==b)){
            resultado = "Triangulo Isosceles";
        }
        else{
            resultado = "Triangulo Escaleno";
        }
    }
    else{
        resultado = "Não é um triangulo";

    }
    return resultado;
}

Call it in method OnClick() :

public void OnClick(View v){
    String resultado = calcular();
    Intent it = new Intent(this, ActSegundaTela.class);
    it.putExtra("RESULTADO", resultado);
    startActivity(it);

}

Note that the calcular() method does not check if the values entered in the EditText are valid, so you should click the button only after you enter integer values in them. See this answer for a possible solution.

    
24.09.2016 / 00:23
0

Put default values in EditText s. 0 is a good value.

When parse the integers, do:

    a = Integer.parseInt("0" + edtA.getText().toString());
    b = Integer.parseInt("0" + edtB.getText().toString());
    c = Integer.parseInt("0" + edtC.getText().toString());
    
02.06.2018 / 19:47
-1

Some of your own, you're in trouble.

Start them in this section:

int a;
int b;
int c;

Use Log.d to check too.

    
23.09.2016 / 23:26