Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid MainActivity and BroadcastReceiver
    primarykey
    data
    text
    <p>I have a problem when trying to reach objects from different classes. I'm on an android project an I've done a socketconnection from my MainActivity class that extends Activity. Then I've made another class called SmsReceiver that extends BroadcastReceiver so I can get messages that are sent to my phone (emulator). Now I need the SmsReceiver class to use the outputstream object from the MainActivity class but I don't know how to reach it. I cannot make a new instance of MainActivity. </p> <p>Can I somehow send the outputstream object to the SmsReceiver class? I hope some of you got some tips, thanks in advance!</p> <hr> <p>Ok now I've gone for the interface solution but I'm stuck again!</p> <p>Here are my classes:</p> <p><strong>MainActivity:</strong></p> <pre><code>public class MainActivity extends Activity implements MainInterface { try { out = new PrintWriter(clientSocket.getOutputStream()); in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); } catch (IOException ex) { System.err.println("Error in creating Streams:" + ex.toString()); return; } public PrintWriter getOutputStream() { return out; } } </code></pre> <p><strong>MainInterface:</strong></p> <pre><code>public interface MainInterface { public PrintWriter getOutputStream(); } </code></pre> <p><strong>SmsReceiver:</strong></p> <pre><code>public class SmsReceiver extends BroadcastReceiver{ MainInterface mainInterface = new MainActivity(); ///// &lt;------ PrintWriter out; //// &lt;-------- @Override public void onReceive(Context context, Intent intent) { mainInterface.getOutputStream(); ////// &lt;------------ // Retrieves a map of extended data from the intent. final Bundle bundle = intent.getExtras(); try { if(bundle != null){ final Object[] pdusObj = (Object[]) bundle.get("pdus"); for(int i = 0; i &lt; pdusObj.length; i++){ 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); out.println("senderNum: "+ senderNum + "; message: " + message); out.flush(); /// these two lines } } } catch (Exception e) { Log.e("SmsReceiver", "Exception smsReceiver" +e); } } } </code></pre> <p>For some reason the PrintWriter that I get through the interface is <em>null</em>, and the inputstream on the desktop (which is the program that should receive the message when doing <strong>out.println()</strong>) doesn't even get the message, it's just blocked at <strong>in.readLine()</strong>. Am I instantiating the interface the wrong way or what's going on?</p> <p>The logcat error print is: <strong>09-15 14:13:55.880: E/SmsReceiver(1361): Exception smsReceiverjava.lang.NullPointerException</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload