Unable to start Activity Componentinfo

Asked

Viewed 4,692 times

0

The app doesn’t work when deploying. It "crasha" straight. Follow the code and 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) 

3 answers

3

You are converting an empty string("") in a whole.

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

This is because nothing has yet been introduced into Edittext.

I suggest you do the following:

Move the code that computes 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 the method OnClick():

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

}

Be aware that the method calcular() does not check whether the values entered in Edittext are valid, you should therefore click the button only after entering integer values.
Look at this reply for a possible solution.

  • This is just a hint of the problem outline, which is still present if the user does not fill the text boxes and click the button

  • @Marcelouchimura The answer refers to this, because the question is not about that. However, I will edit it, added a link to a reply that resolves this.

0

Set default values in EditTexts. 0 is a good value.

The moment you go parse the whole, do:

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

-1

Someone of yours is in trouble.

Start them on this stretch:

int a;
int b;
int c;

Use Log. d to check also.

Browser other questions tagged

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