I'm developing a code in android studio and using bluetooth communication. I want to make sure that if a specific data received by the bluetooth is greater than this value, automatically generate a notification in the cell.
The error:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources android.content.Context.getResources ()'
on a null object reference
Code:
public static Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
/* Esse método é invocado na Activity principal
sempre que a thread de conexão Bluetooth recebe
uma mensagem.
*/
Bundle bundle = msg.getData();
byte[] data = bundle.getByteArray("data");
String dataString= new String(data);
String sep[] = dataString.split(Pattern.quote("E"));
/* Aqui ocorre a decisão de ação, baseada na string
recebida. Caso a string corresponda à uma das
mensagens de status de conexão (iniciadas com --),
atualizamos o status da conexão conforme o código.
*/
if(dataString.equals("---N"))
statusMessage.setText("Erro de conexão.");
else if(dataString.equals("---S"))
statusMessage.setText("Conectado.");
else{
funciona2 = Integer.parseInt(sep[1]);
ActDados actDados = new ActDados();
actDados.notifica();
}
}
};
Function notifica
:
public void notifica(){
NotificationManager nm =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
PendingIntent p = PendingIntent.getActivity(this, 0, new Intent(this,
ActTelaUsuarios1.class), 0);
NotificationCompat.Builder builder = new
NotificationCompat.Builder(this);
builder.setTicker("Ticker Texto")
.setContentTitle("Atenção")
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
if (funciona2 > 60 && funciona2 < 120){
builder.setContentText("Texto notificado 1");
}
else{
builder.setContentText("Texto notificado 2");
}
builder.setContentIntent(p);
Notification n = builder.build();
n.vibrate = new long[]{150, 300, 150, 600};
nm.notify(R.mipmap.ic_launcher, n);
}
I compile the code and move it to the phone, but when it is running the application it warns that there was an error and hangs the app.
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources android.content.Context.getResources ()'
on a null object reference at com.example.vitor.test.Account.notice (ActDados.java:206) at com.example.vitor.test.AccessData $ 1.handleMessage (ActDados.java:288)
The error informs the line where I call the notification function: actDados.notifica();