Error when another class calls method of an Activity

3

In monitoring it sends an SMS to an equipment that responds with another SMS and refresh the screen and at the moment it calls the method to refresh the screen of the error. Error Log:

    01-14 22:45:30.907: E/AndroidRuntime(8661): FATAL EXCEPTION: main
    01-14 22:45:30.907: E/AndroidRuntime(8661): Process: com.example.acquaconnect, PID: 8661
    01-14 22:45:30.907: E/AndroidRuntime(8661): java.lang.RuntimeException: Unable to start receiver com.example.acquaconnect.ReceberSms: java.lang.NullPointerException
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2441)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at android.app.ActivityThread.access$1700(ActivityThread.java:139)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at android.os.Handler.dispatchMessage(Handler.java:102)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at android.os.Looper.loop(Looper.java:136)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at android.app.ActivityThread.main(ActivityThread.java:5097)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at java.lang.reflect.Method.invokeNative(Native Method)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at java.lang.reflect.Method.invoke(Method.java:515)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at dalvik.system.NativeStart.main(Native Method)
    01-14 22:45:30.907: E/AndroidRuntime(8661): Caused by: java.lang.NullPointerException
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:173)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at com.example.acquaconnect.Monitoramento.AtualizaTela(Monitoramento.java:128)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at com.example.acquaconnect.ReceberSms.AtualizaBD(ReceberSms.java:225)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at com.example.acquaconnect.ReceberSms.onReceive(ReceberSms.java:63)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2434)
    01-14 22:45:30.907: E/AndroidRuntime(8661):     ... 10 more

Monitoring Code:

    public class Monitoramento extends ActionBarActivity{

        private static final String CATEGORIA = "acqua";
        private static final String PREF_NAME = "BD";

        TextView monit1,monit2,monit3,monit4,monit5,monit6,monit7,monit8,monit9,monit10,
        monit11,monit12,monit13,monit14,monit15,monit16,monit17;
        String numero ;
        String M1,M2,M3,M4,M5,M6,M7,M8,M9,M10,M11,M12,M13,M14,M15,M16,M17;
        String L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11,L12,L13,L14,L15,L16,L17;

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.monitoramento);

            Button legenda = (Button) findViewById(R.id.butLegenda);
            Button atualizar = (Button) findViewById(R.id.butAtualizar);

            monit1 = (TextView) findViewById(R.id.Monit1);
            monit2 = (TextView) findViewById(R.id.Monit2);
            monit3 = (TextView) findViewById(R.id.Monit3);
            monit4 = (TextView) findViewById(R.id.Monit4);
            monit5 = (TextView) findViewById(R.id.Monit5);
            monit6 = (TextView) findViewById(R.id.Monit6);
            monit7 = (TextView) findViewById(R.id.Monit7);
            monit8 = (TextView) findViewById(R.id.Monit8);
            monit9 = (TextView) findViewById(R.id.Monit9);
            monit10 = (TextView) findViewById(R.id.Monit10);
            monit11 = (TextView) findViewById(R.id.Monit11);
            monit12 = (TextView) findViewById(R.id.Monit12);
            monit13 = (TextView) findViewById(R.id.Monit13);
            monit14 = (TextView) findViewById(R.id.Monit14);
            monit15 = (TextView) findViewById(R.id.Monit15);
            monit16 = (TextView) findViewById(R.id.Monit16);
            monit17 = (TextView) findViewById(R.id.Monit17);

         // VERIFY SHAREDPREFERENCES
                SharedPreferences sp = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
                M1 = sp.getString("monit_1", "Desabilitado");
                M2 = sp.getString("monit_2", "Desabilitado");
                M3 = sp.getString("monit_3", "Desabilitado");
                M4 = sp.getString("monit_4", "Desabilitado");
                M5 = sp.getString("monit_5", "Desabilitado");
                M6 = sp.getString("monit_6", "Desabilitado");
                M7 = sp.getString("monit_7", "Desabilitado");
                M8 = sp.getString("monit_8", "Desabilitado");
                M9 = sp.getString("monit_9", "Desabilitado");
                M10 = sp.getString("monit_10", "Desabilitado");
                M11 = sp.getString("monit_11", "Desabilitado");
                M12 = sp.getString("monit_12", "Desabilitado");
                M13 = sp.getString("monit_13", "Desabilitado");
                M14 = sp.getString("monit_14", "Desabilitado");
                M15 = sp.getString("monit_15", "Desabilitado");
                M16 = sp.getString("monit_16", "Desabilitado");
                M17 = sp.getString("monit_17", "Desabilitado");
                String numcel = sp.getString("num_cel", "");

                L1 = sp.getString("leg_1", "Desabilitado");
                L2 = sp.getString("leg_2", "Desabilitado");
                L3 = sp.getString("leg_3", "Desabilitado");
                L4 = sp.getString("leg_4", "Desabilitado");
                L5 = sp.getString("leg_5", "Desabilitado");
                L6 = sp.getString("leg_6", "Desabilitado");
                L7 = sp.getString("leg_7", "Desabilitado");
                L8 = sp.getString("leg_8", "Desabilitado");
                L9 = sp.getString("leg_9", "Desabilitado");
                L10 = sp.getString("leg_10", "Desabilitado");
                L11 = sp.getString("leg_11", "Desabilitado");
                L12 = sp.getString("leg_12", "Desabilitado");
                L13 = sp.getString("leg_13", "Desabilitado");
                L14 = sp.getString("leg_14", "Desabilitado");
                L15 = sp.getString("leg_15", "Desabilitado");
                L16 = sp.getString("leg_16", "Desabilitado");
                L17 = sp.getString("leg_17", "Desabilitado");

                //Exibe o valor na tela
                monit1.setText(M1);
                monit2.setText(M2);
                monit3.setText(M3);
                monit4.setText(M4);
                monit5.setText(M5);
                monit6.setText(M6);
                monit7.setText(M7);
                monit8.setText(M8);
                monit9.setText(M9);
                monit10.setText(M10);
                monit11.setText(M11);
                monit12.setText(M12);
                monit13.setText(M13);
                monit14.setText(M14);
                monit15.setText(M15);
                monit16.setText(M16);
                monit17.setText(M17);
                numero = numcel;

        }

    //------------BOTAO-ATUALIZAR-------------- 
        public void butAtualizar(View v){
            String mensagem = "*st1";

            Sms sms = new Sms();
            sms.enviarSms(Monitoramento.this, numero,mensagem);

        }

        public void butLegenda(View v){
            finish();
            startActivity(new Intent(getApplicationContext(), Legenda.class));
        }

        public void AtualizaTela(){
            SharedPreferences sp = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
            M1 = sp.getString("monit_1", "Desabilitado");
            M2 = sp.getString("monit_2", "Desabilitado");
            M3 = sp.getString("monit_3", "Desabilitado");
            M4 = sp.getString("monit_4", "Desabilitado");
            M5 = sp.getString("monit_5", "Desabilitado");
            M6 = sp.getString("monit_6", "Desabilitado");
            M7 = sp.getString("monit_7", "Desabilitado");
            M8 = sp.getString("monit_8", "Desabilitado");
            M9 = sp.getString("monit_9", "Desabilitado");
            M10 = sp.getString("monit_10", "Desabilitado");
            M11 = sp.getString("monit_11", "Desabilitado");
            M12 = sp.getString("monit_12", "Desabilitado");
            M13 = sp.getString("monit_13", "Desabilitado");
            M14 = sp.getString("monit_14", "Desabilitado");
            M15 = sp.getString("monit_15", "Desabilitado");
            M16 = sp.getString("monit_16", "Desabilitado");
            M17 = sp.getString("monit_17", "Desabilitado");

            //Exibe o valor na tela
            monit1.setText(M1);
            monit2.setText(M2);
            monit3.setText(M3);
            monit4.setText(M4);
            monit5.setText(M5);
            monit6.setText(M6);
            monit7.setText(M7);
            monit8.setText(M8);
            monit9.setText(M9);
            monit10.setText(M10);
            monit11.setText(M11);
            monit12.setText(M12);
            monit13.setText(M13);
            monit14.setText(M14);
            monit15.setText(M15);
            monit16.setText(M16);
            monit17.setText(M17);

        }
        //-----------BOTÃO DE VOLTAR--------------------    
        @Override
        public void onBackPressed() {
            finish();
            startActivity(new Intent(getApplicationContext(), Usuario.class));
        }
    //-----------FIM BOTÃO DE VOLTAR--------------------


    }//fim

Error Line: In AtualizaTela()

        SharedPreferences sp = getSharedPreferences(PREF_NAME, MODE_PRIVATE);

ReceivingSms Code:

    public class ReceberSms extends BroadcastReceiver {
        private static final String CATEGORIA = "acqua";
        private static final String PREF_NAME = "BD";

        String L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11,L12,L13,L14,L15,L16,L17;
        String M1,M2,M3,M4,M5,M6,M7,M8,M9,M10,M11,M12,M13,M14,M15,M16,M17;

        @Override
        public void onReceive(Context context, Intent intent) {

            Sms sms = new Sms();
            //Lê a mensagem
            SmsMessage msg = sms.receberMensagem(intent);
            String celular = msg.getDisplayOriginatingAddress();
            String mensagem = msg.getDisplayMessageBody();

            // VERIFY SHAREDPREFERENCES
            SharedPreferences sp = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
            String numcel = sp.getString("num_cel", "");


            if(celular.equals(numcel)){
                if(mensagem.contains("MONIT-110")){

                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("mensagem_cel_monit", mensagem);
                    editor.putString("alarme_on", "yes");
                    editor.commit();

                    //chama outra activity
                    intent = new Intent(context, CriaNotificacao.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);

                }else if(mensagem.contains("STATUS-110")){
                    SharedPreferences.Editor editor = sp.edit();

                    editor.putString("mensagem_cel_status", mensagem);
                    editor.commit();

                    AtualizaBD(context);

                }
            } 
        }//fim receive


        public void AtualizaBD(Context context){
            // VERIFY SHAREDPREFERENCES
            SharedPreferences sp = context.getSharedPreferences(PREF_NAME, context.MODE_PRIVATE);
            L1 = sp.getString("leg_1", "Desabilitado");
            L2 = sp.getString("leg_2", "Desabilitado");
            L3 = sp.getString("leg_3", "Desabilitado");
            L4 = sp.getString("leg_4", "Desabilitado");
            L5 = sp.getString("leg_5", "Desabilitado");
            L6 = sp.getString("leg_6", "Desabilitado");
            L7 = sp.getString("leg_7", "Desabilitado");
            L8 = sp.getString("leg_8", "Desabilitado");
            L9 = sp.getString("leg_9", "Desabilitado");
            L10 = sp.getString("leg_10", "Desabilitado");
            L11 = sp.getString("leg_11", "Desabilitado");
            L12 = sp.getString("leg_12", "Desabilitado");
            L13 = sp.getString("leg_13", "Desabilitado");
            L14 = sp.getString("leg_14", "Desabilitado");
            L15 = sp.getString("leg_15", "Desabilitado");
            L16 = sp.getString("leg_16", "Desabilitado");
            L17 = sp.getString("leg_17", "Desabilitado");

            SharedPreferences.Editor editor = sp.edit();
            String mensagemStatus = sp.getString("mensagem_cel_status", "");

            if(L1.equals("Desabilitado")){
                editor.putString("monit_1", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("E01-DES")){editor.putString("monit_1", "Não");editor.commit();
                }else if(mensagemStatus.contains("E01-ATI")){editor.putString("monit_1", "Sim");editor.commit();}
            }

            if(L2.equals("Desabilitado")){
                editor.putString("monit_2", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("E02-DES")){editor.putString("monit_2", "Não");editor.commit();
                }else if(mensagemStatus.contains("E02-ATI")){editor.putString("monit_2", "Sim");editor.commit();}
            }

            if(L3.equals("Desabilitado")){
                editor.putString("monit_3", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("E03-DES")){editor.putString("monit_3", "Não");editor.commit();
                }else if(mensagemStatus.contains("E03-ATI")){editor.putString("monit_3", "Sim");editor.commit();}
            }

            if(L4.equals("Desabilitado")){
                editor.putString("monit_4", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("E04-DES")){editor.putString("monit_4", "Não");editor.commit();
                }else if(mensagemStatus.contains("E04-ATI")){editor.putString("monit_4", "Sim");editor.commit();}
            }

            if(L5.equals("Desabilitado")){
                editor.putString("monit_5", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("E05-DES")){editor.putString("monit_5", "Não");editor.commit();
                }else if(mensagemStatus.contains("E05-ATI")){editor.putString("monit_5", "Sim");editor.commit();}
            }

            if(L6.equals("Desabilitado")){
                editor.putString("monit_6", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("E06-DES")){editor.putString("monit_6", "Não");editor.commit();
                }else if(mensagemStatus.contains("E06-ATI")){editor.putString("monit_6", "Sim");editor.commit();}
            }


            if(L7.equals("Desabilitado")){
                editor.putString("monit_7", "Desabilitado");editor.commit();
            }else{  
                if(mensagemStatus.contains("E07-DES")){editor.putString("monit_7", "Desligado");editor.commit();
                }else if(mensagemStatus.contains("E07-ATI")){editor.putString("monit_7", "Ligado");editor.commit();}
            }

            if(L8.equals("Desabilitado")){
                editor.putString("monit_8", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("E08-DES")){editor.putString("monit_8", "Desligado");editor.commit();
                }else if(mensagemStatus.contains("E08-ATI")){editor.putString("monit_8", "Ligado");editor.commit();}
            }

            if(L9.equals("Desabilitado")){
                editor.putString("monit_9", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("E09-DES")){editor.putString("monit_9", "Desligado");editor.commit();
                }else if(mensagemStatus.contains("E09-ATI")){editor.putString("monit_9", "Ligado");editor.commit();}
            }

            if(L10.equals("Desabilitado")){
                editor.putString("monit_10", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("E10-DES")){editor.putString("monit_10", "Desligado");editor.commit();
                }else if(mensagemStatus.contains("E10-ATI")){editor.putString("monit_10", "Ligado");editor.commit();}
            }

            if(L11.equals("Desabilitado")){
                editor.putString("monit_11", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("E11-DES")){editor.putString("monit_11", "Desligado");editor.commit();
                }else if(mensagemStatus.contains("E11-ATI")){editor.putString("monit_11", "Ligado");editor.commit();}
            }


            if(L12.equals("Desabilitado")){
                editor.putString("monit_12", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("S01-DES")){editor.putString("monit_12", "Manual"); editor.putBoolean("but_1", false);editor.commit();
                }else if(mensagemStatus.contains("S01-ATI")){editor.putString("monit_12", "Automático"); editor.putBoolean("but_1", true);editor.commit();}
            }

            if(L13.equals("Desabilitado")){
                editor.putString("monit_13", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("S02-DES")){editor.putString("monit_13", "Manual"); editor.putBoolean("but_2", false);editor.commit();
                }else if(mensagemStatus.contains("S02-ATI")){editor.putString("monit_13", "Automático"); editor.putBoolean("but_2", true);editor.commit();}
            }

            if(L14.equals("Desabilitado")){
                editor.putString("monit_14", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("S03-DES")){editor.putString("monit_14", "Manual"); editor.putBoolean("but_3", false);editor.commit();
                }else if(mensagemStatus.contains("S03-ATI")){editor.putString("monit_14", "Automático"); editor.putBoolean("but_3", true);editor.commit();}
            }

            if(L15.equals("Desabilitado")){
                editor.putString("monit_15", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("S04-DES")){editor.putString("monit_15", "Manual");editor.putBoolean("but_4", false);editor.commit();
                }else if(mensagemStatus.contains("S04-ATI")){editor.putString("monit_15", "Automático"); editor.putBoolean("but_4", true);editor.commit();}
            }

            if(L16.equals("Desabilitado")){
                editor.putString("monit_16", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("S05-DES")){editor.putString("monit_16", "Manual"); editor.putBoolean("but_5", false);editor.commit();
                }else if(mensagemStatus.contains("S05-ATI")){editor.putString("monit_16", "Automático"); editor.putBoolean("but_6", true);editor.commit();}
            }

            if(L17.equals("Desabilitado")){
                editor.putString("monit_17", "Desabilitado");editor.commit();
            }else{
                if(mensagemStatus.contains("S06-DES")){editor.putString("monit_17", "Manual"); editor.putBoolean("but_6", false);editor.commit();
                }else if(mensagemStatus.contains("S06-ATI")){editor.putString("monit_17", "Automático"); editor.putBoolean("but_6", true);editor.commit();}
            }
            Monitoramento moni = new Monitoramento();
            moni.AtualizaTela();

        }//fim aatualiza

    }//fim

Lines of Errors: In AtualizaBD()

        moni.AtualizarTela();

In onRceive()

        AtualizaBD(context);
    
asked by anonymous 15.01.2015 / 02:54

1 answer

3

The class Monitoramento is Activity , can not be instantiated in the normal way, using new , but through context.startActivity so that the entire creation process is done.

That's why context.getSharedPreferences returns null .

One way to implement what you want is to set a BroadcastReceiver within Activity Monitoramento whose function would be to update the screen. Since BroadcastReceiver is internal you can directly call the AtualizaTela() method.

Define a BroadcastReceiver derived class in your Activity :

public class ActualizaTelaReceiver extends BroadcastReceiver 
{
    @Override
       public void onReceive(Context context, Intent intent) 
       {    
           String action = intent.getAction();
           if(action.equalsIgnoreCase("ACTUALIZA_TELA")){
               //Chame o seu método    
               Monitoramento.ActualizaTela();
           }
       }
}

Create an instance of ActualizaTelaReceiver in method onCreate :

private ActualizaTelaReceiver actualizaTelaReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ....
    ....
    actualizaTelaReceiver = new ActualizaTelaReceiver();
}  

Register BroadcastReceiver on the onResume method of Activity :

@Override
protected void onResume(){
    super.onResume();
    registerReceiver(actualizaTelaReceiver, new IntentFilter("ACTUALIZA_TELA"));
}

Do the "Unregister" method on onPause

@Override
protected void onPause() {

    unregisterReceiver(actualizaTelaReceiver);
    super.onPause();
}

In ReceberSms , instead of instantiating Monitoramento and calling AtualizaTela() , this BroadcastReceiver would be thrown, via sendBroadcast() .

Intent intent = new Intent();
intent.setAction("ACTUALIZA_TELA");
context.sendBroadcast(intent);
    
15.01.2015 / 12:17