AndroidStudio applications are not running

0

I'm new to this in Android Studio, the first applications did install me well, but lately they have not been installed. When creating the App, when it only has the text "Hello World" it is installed well, but as soon as I make a couple of methods and an intent to go to another layout, it does not let me install it. On the mobile it opens and closes alone and in the Android Studio emulator it says "nameApp Has been stoped".

I know it's something I do wrong, but I do not understand which, or where the fault is.

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.x.prueba">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity2"></activity>
    </application>

</manifest>

*. java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    Button siguiente;
    Button salir;
    EditText nombre;
    EditText apellido;
    EditText año;

    int añoVerif;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

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

        siguiente = (Button) findViewById(R.id.btnSiguiente);
        salir = (Button) findViewById(R.id.btnSalir);

        nombre = (EditText) findViewById(R.id.etNombre);
        apellido = (EditText) findViewById(R.id.etApellido);
        año = (EditText) findViewById(R.id.etAño);

        añoVerif = Integer.parseInt(año.getText().toString());

        siguiente.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                if(nombre.toString().isEmpty() || apellido.toString().isEmpty() || año.toString().isEmpty()){
                    Log.e("Vacío", "Error, campos vacíos.");
                    nombre.setHint("Llena todos los campos.");
                    apellido.setHint("Llena todos los campos.");
                    año.setHint("Llena todos los campos.");

                }else {

                    if (añoVerif > 2017 && añoVerif < 1900) {
                        Log.e("Error", "Error en el año introducido");

                    }else{
                        lanzarActividad();
                    }
                }
            }
        });


    }
    public void lanzarActividad(){
        Intent intent = new Intent(MainActivity.this, MainActivity2.class);
        startActivity(intent);
    }

}
    
asked by Urtx 18.11.2017 в 17:03
source

1 answer

1

The problem you are having is in the following line:

añoVerif = Integer.parseInt(año.getText().toString());

When you open your activity, surely the TextView has nothing written, then you order to convert the empty value "" to a int which is not possible. Besides that should not go there placed. The logic of the application, you must obtain the value of the year by pressing the button to perform the relevant checks. Basically the code should be like this:

public class MainActivity extends AppCompatActivity {

Button siguiente;
Button salir;
EditText nombre;
EditText apellido;
EditText año;

int añoVerif;

@Override
protected void onCreate(Bundle savedInstanceState) {

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

    siguiente = (Button) findViewById(R.id.btnSiguiente);
    salir = (Button) findViewById(R.id.btnSalir);

    nombre = (EditText) findViewById(R.id.etNombre);
    apellido = (EditText) findViewById(R.id.etApellido);
    año = (EditText) findViewById(R.id.etAño);

    siguiente.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            if(nombre.toString().isEmpty() || apellido.toString().isEmpty() || año.toString().isEmpty()){
                Log.e("Vacío", "Error, campos vacíos.");
                nombre.setHint("Llena todos los campos.");
                apellido.setHint("Llena todos los campos.");
                año.setHint("Llena todos los campos.");

            }else {
                añoVerif = Integer.parseInt(año.getText().toString());
                if (añoVerif > 2017 && añoVerif < 1900) {
                    Log.e("Error", "Error en el año introducido");

                }else{
                    lanzarActividad();
                }
            }
        }
    });


}
    public void lanzarActividad(){
        Intent intent = new Intent(MainActivity.this, MainActivity2.class);
        startActivity(intent);
    }
}

And if you allow me, I take the liberty of recommending not to use characters like ñ or similar since not all the keyboards have it, and if you ever share some code it could make the task difficult for someone (imagine I answer the code in Chinese: P). But this is just my opinion.

    
answered by 20.11.2017 / 01:49
source