how do I stop a sum?

Asked

Viewed 73 times

1

I’m making an app where I have to pull a sensor (I used the accelerometer) and save the sum of the 3 parameters.

As soon as I try to save that sum the app hangs, I found that the save command that is causing it to crash, someone knows what is wrong ?

from now on thank.

Below is the main code:

package com.example.dfabr.primeiroprograma;

import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import static com.example.dfabr.primeiroprograma.R.id.nomeArqTxt;
import static com.example.dfabr.primeiroprograma.R.id.txtDigitado;

public class telaInformacao extends AppCompatActivity implements SensorEventListener {


    public EditText txtSoma;
    private Button btnSalvarSoma;
    Sensor accelerometer;
    SensorManager sensorManager;






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



        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);


        btnSalvarSoma = (Button) findViewById(R.id.btnSalvarSoma);

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


                try  {
                   // EditText nomeArquivo = (EditText) findViewById(R.id.nomeArqTxt);
                    EditText txtDigitado = (EditText) findViewById(R.id.txtDigitado);
                    FileOutputStream gravarTexto = openFileOutput(txtDigitado.getText().toString(), MODE_APPEND);
                    String conteudoTxt = txtDigitado.getText().toString();
                    gravarTexto.write(conteudoTxt.getBytes());
                    gravarTexto.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
               //Toast.makeText(telaTexto.this, "Arquivo Gravado Com Sucesso", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(telaInformacao.this, MainActivity.class);
                startActivity(intent);
            }
        });

    }


    @Override
    public void onSensorChanged(SensorEvent event) {

        Sensor accelerometer;
        SensorManager sensorManager;

        float sensorX;
        float sensorY;
        float sensorZ;

        sensorX = event.values[0];
        sensorY = event.values[1];
        sensorZ = event.values[2];
        float soma = (event.values[0] + event.values[1] + event.values[2]);



        TextView tx = (TextView) findViewById(R.id.tX);
        TextView ty = (TextView) findViewById(R.id.tY);
        TextView tz = (TextView) findViewById(R.id.tZ);
        TextView ta = (TextView) findViewById(R.id.txtSoma);
        tx.setText("X: " + (sensorX));
        ty.setText("Y: " + (sensorY));
        tz.setText("Z: " + (sensorZ));
        ta.setText("Soma: " + (soma));





    }


    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }


}

error below.

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.dfabr.primeiroprograma, PID: 20680
              java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                  at com.example.dfabr.primeiroprograma.telaInformacao$1.onClick(telaInformacao.java:60)
                  at android.view.View.performClick(View.java:6897)
                  at android.widget.TextView.performClick(TextView.java:12693)
                  at android.view.View$PerformClick.run(View.java:26101)
                  at android.os.Handler.handleCallback(Handler.java:789)
                  at android.os.Handler.dispatchMessage(Handler.java:98)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6944)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
  • Edit your question and put the Logcat log in. Otherwise we will keep trying to guess which error is given. And please don’t post as image, post as text.

2 answers

1

You didn’t initialize yours EditText;

You stated public EditText txtSoma; but needs to initialize.

For example:
txtSoma = findviewbyid(R.id.txtSoma);

0

Try putting a try, at least avoid crashing, can make treatments errors

sensorX = event.values[0];  
sensorY = event.values[1];  
sensorZ = event.values[2];  

float soma = 0.00;
try {    
  soma = (sensorX + sensorY  + sensorZ); 
} 
catch (NumberFormatException e) {
  soma = 0.00;
//tratamento de erro
}
  • continue crashing @Gabriellocalhost

  • tries to debug and see exactly where gives the error, and post here the error message that helps to identify, because this may not be the problem

  • I’ll post in the body of the friend file

  • 1

    discover the personal error, the error is in the write command, in the command is in Edittext pulling from a Textview, Valew personal by help.

  • @Fabrizzio Please post your solution as a new answer and accept it as correct to help other users of the site.

  • @Piovezan, the error is n Try Catch, in a whole, the camando does not serve for this function, but what the right command really did not find

  • @Fabrizzio I do not understand, please clarify better.

  • @Piovezan, the command that I tried to do to save the sum, that is implemented in btnSalvarSoma, and among that code that this the error that was catching the app, I could not solve, only know that when I shoot it the app to lock.

  • @Fabrizzio Edit your question and provide more details on what you have discovered, the purpose of the site is that the questions have an appropriate answer so that others can take advantage of it.

Show 4 more comments

Browser other questions tagged

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