0

Hola estoy practicando un poco de programción en android y me he topado con algo extraño (o eso creo). Cada vez que intento lanzar una nueva activty la aplicación se muere. Estoy intentando realizar una especie de login para una app pero es bastante simple solo solicito el tipo de usuario la contraseña y un nombre de identificación el codigo es el siguiente:

MainActivity:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
EditText txtUser;
EditText txtPass;
Spinner spnTUser;

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

    spnTUser = findViewById(R.id.spnTUser);
    txtUser = findViewById(R.id.txtUser);
    txtPass = findViewById(R.id.txtPass);
}

public void iniciar(View v) {
    String type = spnTUser.getSelectedItem().toString();
    String pass = txtPass.getText().toString();

    switch (type){
        case "Administrator":
            if(pass.equals("a")){
                Intent trypass = new Intent(MainActivity.this, Adminitration.class);
                trypass.putExtra("user", txtUser.getText().toString());
                startActivity(trypass);
            } else {
                Toast err = Toast.makeText(getApplicationContext(), "Usuario Incorrecto", Toast.LENGTH_SHORT);
                err.show();
            }
            break;
        case "Customer":
            if(pass.equals("c")){
                Intent trypasscus = new Intent(this, customer.class);
                trypasscus.putExtra("user", txtUser.getText().toString());
                startActivity(trypasscus);
            } else {
                Toast err = Toast.makeText(getApplicationContext(), "Usuario Incorrecto", Toast.LENGTH_SHORT);
                err.show();
            }
            break;
        case "Support":
            if(pass.equals("s")){
                Intent trypasssu = new Intent(this, support.class);
                trypasssu.putExtra("user", txtUser.getText().toString());
                startActivity(trypasssu);
            } else {
                Toast err = Toast.makeText(getApplicationContext(),  "Usuario Incorrecto", Toast.LENGTH_SHORT);
                err.show();
            }
                break;
        }
   }
}

Layout del MainActivity:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/txtUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="48dp"
        android:layout_marginTop="100dp"
        android:ems="10"
        android:hint="@string/user"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="100dp"
        android:text="@string/user"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="204dp"
        android:text="@string/contrase_a"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/txtPass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="48dp"
        android:layout_marginTop="188dp"
        android:ems="10"
        android:hint="@string/contrase_a"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Spinner
        android:id="@+id/spnTUser"
        android:layout_width="236dp"
        android:layout_height="61dp"
        android:layout_marginBottom="220dp"
        android:layout_marginEnd="4dp"
        android:layout_marginStart="8dp"
        android:entries="@array/val_spin"
        android:prompt="@string/tipo_de_usuario"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.428"
        app:layout_constraintStart_toEndOf="@+id/textView5" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="248dp"
        android:layout_marginStart="16dp"
        android:text="@string/tipo_de_usuario"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:onClick="iniciar"
        android:text="@string/iniciar_sesi_n"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.465"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.741" />

</android.support.constraint.ConstraintLayout>

Manifest:

<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=".Adminitration" >

    </activity>
    <activity android:name=".support">

    </activity>
    <activity android:name=".customer">

    </activity>
</application>

La activity que quiero lanzar, Adminitration por ejemplo:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Adminitration extends AppCompatActivity {

    TextView tvWel;

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

        Bundle extras = getIntent().getExtras();
        String user = extras.getString("user");

        tvWel = findViewById(R.id.tvWelsu);

        tvWel.setText(user);
    }
}

Layout de Adminitration Activity:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Adminitration">

    <TextView
        android:id="@+id/tvWelad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="56dp"
        android:textSize="40sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

y el error que presenta:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: sv.edu.itca.santaana.logindm, PID: 3313
              java.lang.RuntimeException: Unable to start activity ComponentInfo{sv.edu.itca.santaana.logindm/sv.edu.itca.santaana.logindm.Adminitration}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
                  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
                  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
                  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
                  at android.os.Handler.dispatchMessage(Handler.java:106)
                  at android.os.Looper.loop(Looper.java:193)
                  at android.app.ActivityThread.main(ActivityThread.java:6669)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                  at sv.edu.itca.santaana.logindm.Adminitration.onCreate(Adminitration.java:21)
                  at android.app.Activity.performCreate(Activity.java:7136)
                  at android.app.Activity.performCreate(Activity.java:7127)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
                  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
                  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
                  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
                  at android.os.Handler.dispatchMessage(Handler.java:106) 
                  at android.os.Looper.loop(Looper.java:193) 
                  at android.app.ActivityThread.main(ActivityThread.java:6669) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

espero puedan ayudarme muchas gracias

1
  • En la Activity Adminitration estás cargando un layout que no encuentra: setContentView(R.layout.activity_adminitration); debe haber un layout con id: activity_adminitration... del mismo modo en ese layout debe haber un TextView con id tvWelsu... Usas esos elementos en la Activity pero no los veo indentificados en el XML del layout que dices es el de Adminitration ... por cierto, ¿no será más bien Administracion o Administration?
    – A. Cedano
    Commented el 30 ago. 2018 a las 4:50

1 respuesta 1

2

Tienes mal en Adminitration.java el id del TextView. tienes

 tvWel = findViewById(R.id.tvWelsu);

debe ser (así como lo tienes en el xml):

 tvWel = findViewById(R.id.tvWelad);

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.