Question

I am getting findViewById error when I use it for the Button. I know that the function is declared in the Activity class but how do I implement the function without extending the Activity class. I have another class that extends Activity but I cannot use that for my Button Activity since the variables I require are in this Activity.

Here is my class:

public class Activity_Sec extends BroadcastReceiver{
    private static final String LOGCAT = null;
    Button b1;
    final SmsManager sms = SmsManager.getDefault();
    public void onReceive(Context context, Intent intent) {
        final Bundle bundle = intent.getExtras();
        try { if (bundle != null) {
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
            for (int i = 0; i < pdusObj.length; i++) {
                final MediaPlayer mp = MediaPlayer.create(context,R.raw.hospital_alarm);

                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();
                Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);

                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                toast.show();

                String serverNumber= "+9198xxxxxxx";
                b1 = (Button)findViewById(R.id.button1);
                if(senderNum.equals(serverNumber)){
                    Toast toast1 = Toast.makeText(context,"alert message received!!!!",Toast.LENGTH_LONG);
                    toast1.show();
                    mp.start();
                }
                b1.setOnClickListener(new OnClickListener(){ public void onClick(View v){
                    String phoneNumber = "+9198xxxxxxxx";
                    String message = "Ambulance sent!";
                    mp.stop();
                    mp.release();
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(phoneNumber, null, message, null, null);
                }});
            }
        }} catch (Exception e) { Log.e("SmsReceiver", "Exception smsReceiver" +e); }
    }
}
Was it helpful?

Solution

Your class extends BroadcastReceiver while findViewById is a method of Activity class.

You can register for the BroadCast in Activity. Then do whatever is required in the Activity class itself

Activity_Sec sec = new Activity_Sec()
registerReceiver(broadCastReceiver, new IntentFilter(
        "some Action"));

You can make you BroadCasterReceiver as a inner class of Activity class

You can unregister in onPause

Or

You can use a interface as a callback to the activity

Or

Use EventBus

https://github.com/greenrobot/EventBus

 public interface BReceiver {

 public void returnData(String value); // note type is string

}

In onReceive

BReceiver br = (BReceiver) context;

Then

br.returnData("your string data")

Then finally in your Activity implement the interface.

public class MainActivity extends Activity implements BReceiver{

@Override
public void returnData(String value) {
    // TODO Auto-generated method stub

}

OTHER TIPS

My advice: Take out all that OnClickListener stuff. Just declare a handler function that implements the right interface and link it in the XML.

public void handleAmbulanceClick(View v) {
  // ... what's in onClick now ...
}

Then, in your layout/your_activity.XML file:

<Button
  android:id="@+id/button1"
  ... your other atttributes ...
  android:doClick="handleAmbulanceClick" />

put the id into bundle and use it from there

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top