JPgiq
JPgiq

Reputation: 84

Android crash after using a broadcast on a secondary activity

My aplication has a Log activity where the user enters a cellphone number. He can send a code "1234020" with the button ("ENVIAR") to that number and change a TextView with the content and the number of an incoming message.

With the button "SIGUIENTE" the application must to go to the second activity LucesAlarma. There, they modify a TextView with the telephone number of the first activity and send (With the button "ENVIARLA") and receive messages.

The button "Atras" should return to the first activity. The problem is that the application crashes after pushing the button "Siguiente" in MainActivity. I believe there is a problem in the Manifest.

Main Activity:

public class MainActivity extends AppCompatActivity {
    Button ENVIAR,SIGUIENTE;
    EditText NUMEROT;
    TextView CONTENIDO;
    IntentFilter intentFilter;

    private BroadcastReceiver intentReciever = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            String MENSAJE = intent.getExtras().getString("mensaje");
            String NUMERODELMENSAJE  = intent.getExtras().getString("numero");
            CONTENIDO=(TextView)findViewById(R.id.tvSMScontenido);
            CONTENIDO.setText(MENSAJE+NUMERODELMENSAJE);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intentFilter = new IntentFilter();
        intentFilter.addAction("SMS_RECEIVED_ACTION");

        ENVIAR     = (Button) findViewById(R.id.bEnviarMA);
        SIGUIENTE  = (Button)findViewById(R.id.bSiguienteMA);
        NUMEROT    = (EditText)findViewById(R.id.etNumeroTelf);
        ENVIAR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ELMENSAJE = "1234020";
                String ELNUMERO = NUMEROT.getText().toString();
                EnviarSMS(ELMENSAJE, ELNUMERO);
                Toast.makeText(getApplicationContext(), "Ya esta ya", Toast.LENGTH_LONG).show();
            }
        });

        SIGUIENTE.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intento = new Intent(MainActivity.this, LucesAlarma.class);
                intento.putExtra("numero", NUMEROT.getText().toString());
                startActivity(intento);
            }
        });
    }

    @Override
    protected void onResume() {
        registerReceiver(intentReciever, intentFilter);
        super.onResume();
    }

    @Override
    protected void onPause() {
        unregisterReceiver(intentReciever);
        super.onPause();
    }

    protected void EnviarSMS(String elmensaje, String elnumero) {

        SmsManager SMS = SmsManager.getDefault();
        SMS.sendTextMessage(elnumero, null, elmensaje, null, null);
    }
}

Second Activity (LucesAlarma)

public class LucesAlarma extends AppCompatActivity {
    TextView NUMEROLA,MENSAJELA;
    Button ENVIARLA,ATRAS;
    IntentFilter intentFilterLA;

    private BroadcastReceiver intentRecieverLA = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent){

            String MENSAJE = intent.getExtras().getString("mensaje");
            String NUMERODELMENSAJE  = intent.getExtras().getString("numero");
            MENSAJELA=(TextView)findViewById(R.id.tvMensajeLA);
            MENSAJELA.setText(MENSAJE+NUMERODELMENSAJE);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_luces_alarma);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Intent intent = getIntent();
        final String numeropos =intent.getStringExtra("numero");
        NUMEROLA=(TextView)findViewById(R.id.tvNumeroLA);
        NUMEROLA.setText(numeropos);

        ENVIARLA=(Button)findViewById(R.id.bEnviarLA);
        ENVIARLA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ELMENSAJE = "1234020";
                String ELNUMERO = numeropos;
                EnviarSMS(ELMENSAJE, ELNUMERO);
                Toast.makeText(getApplicationContext(), "Enviado", Toast.LENGTH_LONG).show();
            }
        });

        ATRAS=(Button)findViewById(R.id.bAtras);
        ATRAS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentoLA = new Intent(LucesAlarma.this, MainActivity.class);
                startActivity(intentoLA);
            }
        });

    }

    @Override
    protected void onResume() {
        registerReceiver(intentRecieverLA,intentFilterLA);
        super.onResume();
    }

    @Override
    protected void onPause() {
        unregisterReceiver(intentRecieverLA);
        super.onPause();
    }

    protected void EnviarSMS(String elmensaje, String elnumero) {

        SmsManager SMS = SmsManager.getDefault();
        SMS.sendTextMessage(elnumero, null, elmensaje, null, null);
    }
}

SMS Reciver java class:

public class ReceptorSMS extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        SmsMessage[]messages=null;
        String str = "";
        String num = "";
        String men = "";
        if(bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            assert pdus != null;
            messages = new SmsMessage[pdus.length];
            for (int i=0 ; i<messages.length;i++) {
                messages[i]=SmsMessage.createFromPdu((byte[])pdus[i]);
                num = messages[i].getDisplayOriginatingAddress();
                str += "Mensaje de" +messages[i].getOriginatingAddress();
                str += ":";
                str += messages[i].getMessageBody();
                str += "\n";
                men = messages[i].getMessageBody();
            }
            //           Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction("SMS_RECEIVED_ACTION");
            broadcastIntent.putExtra("sms", str);
            broadcastIntent.putExtra("mensaje", men);
            broadcastIntent.putExtra("numero", num);
            context.sendBroadcast(broadcastIntent);
        }
    }
}

And the manifest, I can't identify the problem :'v

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.josue.smartwasip">

    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LucesAlarma"
            android:label="@string/title_activity_luces_alarma"
            android:theme="@style/AppTheme.NoActionBar">

            <intent-filter>
                <action android:name="com.example.josue.smartwasi" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>
        <receiver android:name=".ReceptorSMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

Here is the exption:

03-08 15:39:15.059 1385-1385/com.example.josue.smartwasip E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.josue.smartwasip, PID: 1385
    java.lang.RuntimeException: Unable to resume activity {com.example.josue.smartwasip/com.example.josue.smartwasip.LucesAlarma}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.IntentFilter.writeToParcel(android.os.Parcel, int)' on a null object reference
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.IntentFilter.writeToParcel(android.os.Parcel, int)' on a null object reference
        at android.app.ActivityManagerProxy.registerReceiver(ActivityManagerNative.java:3008)
        at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1184)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1152)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1146)
        at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:554)
        at com.example.josue.smartwasip.LucesAlarma.onResume(LucesAlarma.java:76)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
        at android.app.Activity.performResume(Activity.java:6312)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Upvotes: 0

Views: 1779

Answers (1)

George Mulligan
George Mulligan

Reputation: 11903

You are only ever declaring IntentFilter intentFilterLA; in LucesAlarma. You also need to assign it a value like you do in MainActivity.onCreate() for the IntentFilter there.

Add these two lines to LucesAlarma.onCreate()

intentFilterLA = new IntentFilter();
intentFilterLA.addAction("SMS_RECEIVED_ACTION");

Upvotes: 2

Related Questions