-1

It says null to my message, and i want to take message that i write from another class, is it possible ? Can anyone help to see what's my error ? I've tried moving button to this class too, but it wont work either,

public class ReceiveMessage extends BroadcastReceiver { 

private Button btnchange;
EditText qwer;
private Context ctx;
EditText text;
TextView asd;


static TextView messageBox;
static String phoneNumber;
private EditText pesan;

public void onReceive(Context context, Intent intent) {

    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        Object[] messages = (Object[]) bundle.get("pdus");
        SmsMessage[] sms = new SmsMessage[messages.length];

        for (int n = 0; n < messages.length; n++)

        {
            sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
            break;
        }

        for (SmsMessage msg : sms)              
        {           
            String msga = msg.getMessageBody();
            String phoneNumber1 = msg.getOriginatingAddress();
            String name = getContactName(context, phoneNumber1);

            if (name != null && !name.equals("null"))

            {
                SmsManager smsManager = SmsManager.getDefault();

                String sendTo = phoneNumber1;

 // i want to locate my message that i write from another class
                asd = (TextView) findViewById (R.id.textView1);
                qwer = (EditText) findViewById (R.id.editText1);


            try {                       
// here's the message that i want to send but the program says null inside my msg 
                String smsMessage = "Hi " +name+ "\n\n" +qwer.getText().toString()+ "\n\nThx";

                smsManager.sendTextMessage(sendTo, null, smsMessage, null,
                        null);  

            }catch(Exception ex){
                ex.printStackTrace();
                System.out.println("errror karena " +ex.getMessage());
            }


            }

            }

    }
    private EditText findViewById (int e) {

    return  null;
}

}
Pang
  • 9,564
  • 146
  • 81
  • 122
noob
  • 11
  • 4
  • Whats the value you are getting after this call getContactName. Do you see any value at all? – Shiv Jan 06 '15 at 02:18
  • 1
    why you are using `findViewById` in `BroadcastReceiver` ? – ρяσѕρєя K Jan 06 '15 at 02:21
  • @Shiv : yes, there are some code that i delete from this because its too long, do you want to see it ? i can post it. yes i can see value from getContactName, the problem is when i received message, my message that i write from another xml file, doesn't send it back, it says null – noob Jan 06 '15 at 02:29
  • @ρяσѕρєяK because i want to take variable from another class xml, is it possible ? qwer = (EditText) findViewById (R.id.editText1); – noob Jan 06 '15 at 02:31

1 Answers1

0

You can not access the another class inflated view from this class. You need to check if you've declare this receiver in the manifest. Have a look on this stack overflow question. Android - SMS Broadcast receiver

Community
  • 1
  • 1
Sanjeet A
  • 5,171
  • 3
  • 23
  • 40
  • Yes i already declare the receiver and it works, but when my receiver send sms back, the message that send is null, and can you explain "can not access the another class inflated view from this class" because i'm still beginner so i don't really understand. – noob Jan 06 '15 at 06:25
  • Your qwer.getText().toString() Will cause NullPointerException , Your EditText object is not creating. Yous this Receiver as inner class of your Activity and do the initilization fo TexTtview and EditText in on create, It will be more convenient for you. – Sanjeet A Jan 06 '15 at 06:56
  • I tried but the program just shutdown when I send my sms, and I already changed the path in manifest to my other class here's the logcat 01-06 15:45:41.908: E/AndroidRuntime(27510): java.lang.RuntimeException: Unable to instantiate receiver percobaan.safetydriving.ChangeMessage$Receiver: java.lang.InstantiationException: can't instantiate class percobaan.safetydriving.ChangeMessage$Receiver; no empty constructor – noob Jan 06 '15 at 07:47