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.
– Piovezan