Android studio ERROR java.lang.Nullpointerexception

Asked

Viewed 393 times

0

I’m developing a code in android studio and using bluetooth communication. I want to make that if a specific data received by bluetooth is greater than such value, automatically generates a notification on mobile.

The mistake:

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 pass it to the mobile, but when running the application it warns that there was 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.example.Vitor.teste.Actdados.notifies(Actdados.java:206) at com.example.Vitor.teste.Actdata$1.handleMessage(Actdados.java:288)

Error tells the line where to call the notification function : actDados.notifica();

  • The one on line 206 of the Actdata file.java ?

  • The problem seems to be running n.vibrate = new long[]{150, 300, 150, 600};. What are you doing on that line?

No answers

Browser other questions tagged

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