Error opening Activity: The <application> stopped

Asked

Viewed 4,140 times

0

I have a file VigasFragment.java with a button that when touched should open the Activity VigMetBiapsb.java, however some error occurs, closing the application.

Vigasfragment.java:

public class VigasFragment extends Fragment {

Button btnBiapsb;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.vigas_fragment, container, false);

    // ////////////////////////////////////////////////////////////////////////////////
    btnBiapsb = (Button) rootView.findViewById(R.id.biapsb);
    btnBiapsb.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intBiapsb = new Intent(v.getContext(), VigMetBiapsb.class);
            startActivityForResult(intBiapsb, 0);
        }
    });
    // ////////////////////////////////////////////////////////////////////////////////

        return rootView;

    }// fecha onCreateView

}

Vigmetbiapsb.java:

    public class VigMetBiapsb extends Activity {

        int porctAlt, porctLarg;

        EditText edtVao = (EditText) findViewById(R.id.vao);
        final double edtVaoNum = Double.parseDouble(edtVao.getText().toString());

        Button calcBiapsb = (Button) fi

ndViewById(R.id.calc_biapsb);

    TextView secaoBiapsb;

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

        // Spinner
        Spinner spnCargas = (Spinner) findViewById(R.id.spn_cargas);
        ArrayAdapter<CharSequence> spnAdapter = ArrayAdapter.createFromResource(this, R.array.str_cargas, R.layout.spinner_style);
        spnAdapter.setDropDownViewResource(R.layout.spinner_dropdown_style);
        spnCargas.setAdapter(spnAdapter);
        // Spinner

        spnCargas.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {

                    public void onItemSelected(AdapterView<?> spnAdpView, View v, int carga, long id) {

                        if(spnAdpView.getItemAtPosition(carga).toString() == "Cargas pequenas"){ porctAlt = 4; porctLarg = 40; }
                        else if(spnAdpView.getItemAtPosition(carga).toString() == "Cargas médias"){ porctAlt = 5; porctLarg = 50; }
                        else { porctAlt = 6; porctLarg = 60; }

                    }// fecha onItemSelected

                    public void onNothingSelected(AdapterView<?> arg0){}
                }//fecha OnItemSelectedListener
        ); // fecha setOnItemSelectedListener

        calcBiapsb.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(edtVaoNum != 0){
                    double alt = edtVaoNum * porctAlt;
                    double larg = alt / 100 * porctLarg;

                    secaoBiapsb = (TextView) findViewById(R.id.valorsec);
                    secaoBiapsb.setText(String.valueOf(alt)+" x "+String.valueOf(larg));
                }
                else {
                    Toast.makeText(getApplicationContext(), "Informe o tamanho do vão", Toast.LENGTH_SHORT).show();
                 }
            }
        });

    } //fecha onCreate
}

I believe the error is in the code of VigMetBiapsb.java because when I leave only the method onCreate() within the class VigMetBiapsb, to Activity opens normally.

Errors of the Logcat at the touch of the button btnBiapsb

Errors of the Logcat at the touch of the button btnBiapsb

  • Edeilton, we can’t guess the problem without you including the stacktrace about the mistake. If you can, include it in your question.

  • You’re right, @Wakim. I’ve edited the question, I think it’s clearer now.

2 answers

1


The problem is that this initiating their View's in their declaration within the Activity. What makes the method findViewById be called on the builder of your Activity. And at that moment yours View has not yet been built, generating the NullPointerException.

Migrate this entire startup to onCreate, after the setContentView, as well as the other initializations you do.

Change of:

public class VigMetBiapsb extends Activity {

    EditText edtVao = (EditText) findViewById(R.id.vao);
    final double edtVaoNum = Double.parseDouble(edtVao.getText().toString());
    Button calcBiapsb = (Button) findViewById(R.id.calc_biapsb);

    // ...
}

To:

public class VigMetBiapsb extends Activity {

    int porctAlt, porctLarg;
    double edtVaoNum;

    EditText edtVao;
    Button calcBiapsb;
    TextView secaoBiapsb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Sua inicializacao atual...

        edtVao = (EditText) findViewById(R.id.vao);

        // Isso nao vai dar erro? edtVao esta preenchido nesse momento?
        edtVaoNum = Double.parseDouble(edtVao.getText().toString());

        calcBiapsb = (Button) findViewById(R.id.calc_biapsb);
    }
}
  • The error was really because of the startup, only I’m still facing two problems: The variable edtVaoNum is not filled in at the time it is initialized, then the Activity does not open yet. E if I use the field android:text="" of this EditText with any value, the Activity opens, but even if I modify the value after (in the user interface) the calculation is done with the value entered in the code.

  • The same occurs with Spinner that at startup has the value "Large loads", which fits the else { porctAlt = 6; porctLarg = 60; }. So even after I select another option in Spinner, the calculation is always done with porctAlt = 6; porctLarg = 60;.

  • The problem of Spinner is that you’re comparing String in the wrong way (with ==), use the method equals to compare objects. In the case of edtVaoNum you should not initialize it in onCreate, because the value has not yet been entered by the user. You must set the value edtVaoNum only within the onClick of calcBiapsb.

  • It worked and I understood perfectly, thank you very much!

-1

I don’t think you need to give this startActivityForResult(intBiapsb, 0); Yes only one starActivity(intBiapsb);

Browser other questions tagged

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