Error opening Activity: Application stopped

0

I have a VigasFragment.java file with a button that when touched should open 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 think the error is in the code of VigMetBiapsb.java because when I leave only the onCreate() method within the VigMetBiapsb class, Activity normally opens.

LogCat errors by touching the btnBiapsb

LogCat Errors by touching the btnBiapsb

    
asked by anonymous 23.11.2014 / 19:55

2 answers

1

The problem is that you are initializing your View's in the declaration of them within Activity . This causes the findViewById method to be called in the constructor of its Activity . And at that time, its View has not yet been built, generating NullPointerException .

Migrate this entire initialization to onCreate , after setContentView , as well as any other initializations you make.

Change from:

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);
    }
}
    
23.11.2014 / 21:07
-1

I do not think you need to give this startActivityForResult (intBiapsb, 0); and yes only a starActivity (intBiapsb);

    
23.11.2017 / 14:23