Activity giving crash

Asked

Viewed 57 times

0

When I start Activity "Data" the screen turns white the program gives a crash, but if I comment all the programming of the same the program does not crash, but the screen is blank and what is in the . xml does not appear. What I am doing wrong and how should I make it work?

Previous Activity button that starts Activity "Data":

next = (Button) findViewById(R.id.prosseguir);
    intentNext = new Intent(MenuInicial.this, Dados.class);

    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Float baseMt = getIntent().getExtras().getFloat("baseM");
            intentNext.putExtra("baseM",baseMt);
            startActivity(intentNext);

        }
    });

Activity "Data":

package com.mateus.ligacoestubulares;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class Dados extends AppCompatActivity {

    String [] AçoMontante = {"SAE 1020", "E-155"};
    String [] AçoBanzo = {"SAE 1020", "E-155"};
    String [] EspessuraT1 = {"0,75 mm", "0,90 mm", "0,95 mm"};
    String [] EspessuraTo = {"0,75 mm", "0,90 mm", "0,95 mm"};
    EditText campoFy1;
    EditText campoFyo;
    EditText campoT1;
    EditText campoTo;
    EditText normalM;
    EditText normalB;
    EditText momentoB;
    EditText teta;
    Button next;
    Intent intentNext;
    Bundle bundle;
    Double t1;
    Double to;
    Double f1;
    Double fo;

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

        Spinner spinnerMont = (Spinner) findViewById(R.id.spinnerM);

        ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, AçoMontante);
        spinnerMont.setAdapter(arrayAdapter1);

        campoFy1 = (EditText) findViewById(R.id.fy1);

        String fy1 = Double.toString(choiceM());

        campoFy1.setText(fy1);

        Spinner spinnerBanzo = (Spinner) findViewById(R.id.spinnerB);

        ArrayAdapter<String> arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, AçoBanzo);
        spinnerBanzo.setAdapter(arrayAdapter2);

        campoFyo = (EditText) findViewById(R.id.fyo);

        String fyo = Double.toString(choiceB());

        campoFyo.setText(fyo);

        Spinner spinner1 = (Spinner) findViewById(R.id.spinnerT1);

        ArrayAdapter<String> arrayAdapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, EspessuraT1);
        spinner1.setAdapter(arrayAdapter3);

        campoT1 = (EditText) findViewById(R.id.t1);

        String espessura1 = Double.toString(choiceT1());

        campoT1.setText(espessura1);

        Spinner spinner0 = (Spinner) findViewById(R.id.spinnerTo);

        ArrayAdapter<String> arrayAdapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, EspessuraTo);
        spinner0.setAdapter(arrayAdapter4);

        campoTo = (EditText) findViewById(R.id.to);

        String espessura0 = Double.toString(choiceTo());

        campoTo.setText(espessura0);

        next = (Button) findViewById(R.id.prosseguir);
        intentNext = new Intent(Dados.this, ConferenciaDosDados.class);

        normalM = (EditText) findViewById(R.id.NormalM);
        normalB = (EditText) findViewById(R.id.NormalB);
        momentoB = (EditText) findViewById(R.id.Momento);
        teta = (EditText) findViewById(R.id.Teta1);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String normal = normalM.getText().toString();
                String normal1 = normalB.getText().toString();
                String momento = momentoB.getText().toString();
                String teta1 = teta.getText().toString();
                String resistenciaM = campoFy1.getText().toString();
                String resistenciaB = campoFyo.getText().toString();
                String espessuraM = campoT1.getText().toString();
                String espessuraB = campoTo.getText().toString();
                bundle.putString("NormalM",normal);
                bundle.putString("NormalB",normal1);
                bundle.putString("MomentB",momento);
                bundle.putString("Teta1",teta1);
                bundle.putString("fy1",resistenciaM);
                bundle.putString("fyo",resistenciaB);
                bundle.putString("t1",espessuraM);
                bundle.putString("to",espessuraB);
                intentNext.putExtras(bundle);
                startActivity(intentNext);

            }
        });
    }

    public double choiceM() {

        if (AçoMontante.equals("SAE 1020")) {
            f1 = 250.0;
        } else if (AçoMontante.equals("E-155")) {
            f1 = 300.0;
        }
        return f1;
    }

    public double choiceB() {

        if (AçoBanzo.equals("SAE 1020")) {
            fo = 250.0;
        } else if (AçoBanzo.equals("E-155")) {
            fo = 300.0;
        }
        return fo;
    }

    public double choiceT1() {

        if (EspessuraT1.equals("0,75 mm")) {
            t1 = 0.75;
        } else if (EspessuraT1.equals("0,90 mm")) {
            t1 = 0.90;
        } else if (EspessuraT1.equals("0,95 mm")) {
            t1 = 0.95;
        }
        return t1;
    }

    public double choiceTo() {

        if (EspessuraTo.equals("0,75 mm")) {
            to = 0.75;
        } else if (EspessuraTo.equals("0,90 mm")) {
            to = 0.90;
        } else if (EspessuraTo.equals("0,95 mm")) {
            to = 0.95;
        }
        return to;
    }
}

xml of Activity "Data":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mateus.ligacoestubulares.Dados">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/dados"
                android:id="@+id/tituloDados"
                android:layout_gravity="center"
                android:gravity="center"
                android:textSize="20sp"
                android:textStyle="bold"
                android:layout_weight="1"
                android:layout_margin="20dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Normal"
                android:layout_gravity="center"
                android:layout_margin="10dp"/>

            <LinearLayout
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_weight="1">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="25dp"
                    android:text="@string/N"
                    android:textSize="18sp"
                    android:layout_weight="1"/>

                <EditText
                    android:id="@+id/NormalM"
                    android:layout_width="250dp"
                    android:layout_height="25dp"
                    android:maxWidth="100dp"
                    android:maxHeight="30dp"
                    android:textColorHint="#838B8B"
                    android:background="#BCD2EE"
                    android:layout_weight="1"
                    android:inputType="numberDecimal"
                    android:layout_margin="10dp"
                    android:layout_gravity="center"/>

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/NormalB"
                android:layout_gravity="center"
                android:layout_margin="10dp"/>

            <LinearLayout
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_weight="1">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="25dp"
                    android:text="@string/No.Sd"
                    android:textSize="18sp"
                    android:layout_weight="1"/>

                <EditText
                    android:id="@+id/NormalB"
                    android:layout_width="250dp"
                    android:layout_height="25dp"
                    android:maxWidth="100dp"
                    android:maxHeight="30dp"
                    android:textColorHint="#838B8B"
                    android:background="#BCD2EE"
                    android:layout_weight="1"
                    android:inputType="numberDecimal"
                    android:layout_margin="10dp"
                    android:layout_gravity="center"/>

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/MomentoB"
                android:layout_gravity="center"
                android:layout_margin="10dp"/>

            <LinearLayout
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_weight="1">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="25dp"
                    android:text="@string/Mo.Sd"
                    android:textSize="18sp"
                    android:layout_weight="1"/>

                <EditText
                    android:id="@+id/Momento"
                    android:layout_width="250dp"
                    android:layout_height="25dp"
                    android:maxWidth="100dp"
                    android:maxHeight="30dp"
                    android:textColorHint="#838B8B"
                    android:background="#BCD2EE"
                    android:layout_weight="1"
                    android:inputType="numberDecimal"
                    android:layout_margin="10dp"
                    android:layout_gravity="center"/>

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Angulo"
                android:layout_gravity="center"
                android:layout_margin="10dp"/>

            <LinearLayout
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_weight="1">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="25dp"
                    android:text="@string/Teta"
                    android:textSize="18sp"
                    android:layout_weight="1"/>

                <EditText
                    android:id="@+id/Teta1"
                    android:layout_width="250dp"
                    android:layout_height="25dp"
                    android:maxWidth="100dp"
                    android:maxHeight="30dp"
                    android:textColorHint="#838B8B"
                    android:background="#BCD2EE"
                    android:layout_weight="1"
                    android:inputType="numberDecimal"
                    android:layout_margin="10dp"
                    android:layout_gravity="center"/>

            </LinearLayout>

            <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/f1"
            android:layout_gravity="center"
            android:layout_margin="10dp"/>

            <Spinner
                android:id="@+id/spinnerM"
                android:layout_width="250dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:layout_margin="10dp">
            </Spinner>

        <LinearLayout
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_weight="1">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="25dp"
                android:text="@string/fy1"
                android:textSize="18sp"
                android:layout_weight="1"/>

            <EditText
                android:id="@+id/fy1"
                android:layout_width="250dp"
                android:layout_height="25dp"
                android:maxWidth="100dp"
                android:maxHeight="30dp"
                android:textColorHint="#838B8B"
                android:background="#BCD2EE"
                android:layout_weight="1"
                android:inputType="numberDecimal"
                android:layout_margin="10dp"
                android:layout_gravity="center"/>

        </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/fo"
                android:layout_gravity="center"
                android:layout_margin="10dp"/>

            <Spinner
                android:id="@+id/spinnerB"
                android:layout_width="250dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:layout_margin="10dp">
            </Spinner>

        <LinearLayout
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_weight="1">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="25dp"
                android:text="@string/fyo"
                android:textSize="18sp"
                android:layout_weight="1"/>

            <EditText
                android:id="@+id/fyo"
                android:layout_width="250dp"
                android:layout_height="25dp"
                android:maxWidth="100dp"
                android:maxHeight="30dp"
                android:textColorHint="#838B8B"
                android:background="#BCD2EE"
                android:layout_weight="1"
                android:inputType="numberDecimal"
                android:layout_margin="10dp"
                android:layout_gravity="center"/>

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/T1"
                android:layout_gravity="center"
                android:layout_margin="10dp"/>

            <Spinner
                android:id="@+id/spinnerT1"
                android:layout_width="250dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:layout_margin="10dp">
            </Spinner>

            <LinearLayout
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_weight="1">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="25dp"
                    android:text="@string/t1"
                    android:textSize="18sp"
                    android:layout_weight="1"/>

                <EditText
                    android:id="@+id/t1"
                    android:layout_width="250dp"
                    android:layout_height="25dp"
                    android:maxWidth="100dp"
                    android:maxHeight="30dp"
                    android:textColorHint="#838B8B"
                    android:background="#BCD2EE"
                    android:layout_weight="1"
                    android:inputType="numberDecimal"
                    android:layout_margin="10dp"
                    android:layout_gravity="center"/>

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/To"
                android:layout_gravity="center"
                android:layout_margin="10dp"/>

            <Spinner
                android:id="@+id/spinnerTo"
                android:layout_width="250dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:layout_margin="10dp">
            </Spinner>

            <LinearLayout
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_weight="1">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="25dp"
                    android:text="@string/to"
                    android:textSize="18sp"
                    android:layout_weight="1"/>

                <EditText
                    android:id="@+id/to"
                    android:layout_width="250dp"
                    android:layout_height="25dp"
                    android:maxWidth="100dp"
                    android:maxHeight="30dp"
                    android:textColorHint="#838B8B"
                    android:background="#BCD2EE"
                    android:layout_weight="1"
                    android:inputType="numberDecimal"
                    android:layout_margin="10dp"
                    android:layout_gravity="center"/>

            </LinearLayout>

            <Button
                android:layout_height="30dp"
                android:layout_width="250dp"
                android:layout_gravity="center"
                android:text="@string/botao1"
                android:id="@+id/prosseguir"
                android:textColor="#FFFFFF"
                android:background="@color/colorPrimary"
                android:textSize="18sp"
                android:layout_margin="20dp"
                android:layout_weight="1"/>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

LOG:

08-28 13:44:28.375 6764-6764/com.mateus.ligacoestubulares E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.mateus.ligacoestubulares, PID: 6764
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mateus.ligacoestubulares/com.mateus.ligacoestubulares.Dados}: java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
     at android.app.ActivityThread.-wrap11(ActivityThread.java)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:5417)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
     at com.mateus.ligacoestubulares.Dados.choiceM(Dados.java:125)
     at com.mateus.ligacoestubulares.Dados.onCreate(Dados.java:46)
     at android.app.Activity.performCreate(Activity.java:6237)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  • It would be interesting to put the error too.

  • Please post the Logcat content that appears after the error in the app, please.

2 answers

0

We have some problems here. Within the method choiceM() you are doing the check if the vector AçoMontante is equal to a string and will always give false in both conditions, because they are different types. This leads to the other problem, as you did not initialize the variable f1, she is of value null and you’re returning the value null as a parameter for the method Double.toString() and this is making your app "crash" because null cannot be converted to Double. Note that the same problems will occur in the methods choiceB(), choiceT1() and choiceTo().

CURRENTLY

String [] AçoMontante = {"SAE 1020", "E-155"};
Double f1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    String fy1 = Double.toString(choiceM());
    ...
}

public double choiceM() {

    if (AçoMontante.equals("SAE 1020")) {
        f1 = 250.0;
    } else if (AçoMontante.equals("E-155")) {
        f1 = 300.0;
    }
    return f1;
}

SOLUTION

public double choiceM() {
    /* A primeira condição sempre vai ser true se você
       não estiver pensando em algo diferente desse
       tipo de verificação.
    */
    if (Arrays.asList(AçoMontante).contains("SAE 1020")) {
        f1 = 250.0;
    } else if (Arrays.asList(AçoMontante).contains("E-155")) {
        f1 = 300.0;
    } else {
        /* Você pode inicializar sua variável aqui,
           assim você sempre terá um valor para
           caso você precise processar a variável
        */
        f1 = 0.0;
    }
    return f1;
}

-2

08-28 13:44:28.375 6764-6764/com.mateus.ligacoestubulares E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.mateus.ligacoestubulares, PID: 6764
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mateus.ligacoestubulares/com.mateus.ligacoestubulares.Dados}: java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
                                                                            at com.mateus.ligacoestubulares.Dados.choiceM(Dados.java:125)
                                                                            at com.mateus.ligacoestubulares.Dados.onCreate(Dados.java:46)
                                                                            at android.app.Activity.performCreate(Activity.java:6237)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  • Do not use the "Answer" button to add data to the question, the answer field is to put the solution only.

Browser other questions tagged

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