Note that there are some explanatory texts on larger screens.

plurals
  1. PONot allowed to call my own methods ("static" mystery)
    text
    copied!<p>I have a BroadcastReceiver as a separate class file, listening to sensor actions from a service in the background. I want it to show text on the screen on occassions.</p> <p>I want the BroadcastClass to call a method in my Activity class which takes care of the string to be shown. But my BroadcastReceiver class cannot call methods which are not static(?) <em>"Cannot make a static reference to the non-static method showString(String) from the type myActivity"</em></p> <p>And if I make the particular method static in my Activity class, it effectively looses contact with the UI of its own activity by not finding findViewById anymore <em>"Cannot make a static reference to the non-static method findViewById(int) from the type Activity".</em></p> <p>In another small app, I had a BroadcastReceiver as an internal class inside an Activity class file. Then I had no problems accessing the UI from it directly. I then had the relevant Views declared as class file fields. But I'd like to structure things better, separating UI from broadcast receiving and analysis. It gets so messy to have the entire application source code in one and the same file.</p> <p>What does it mean to create instances of an Activity? Should I do that in the Activity itself or in the BroadcastReceiver? I don't want more than one instance of my UI, but I might need to access it from several different classes.</p> <p>EDIT ADDITION: I try this singleton pattern from a book, but myActivity cannot be instantiated, access to constructor not allowed. Crash before onCreate is entered:</p> <pre><code>public class MyStartupActivity extends Activity { private static MyStartupActivity uniqueInstance; private MyStartupActivity() {} public static MyStartupActivity instantiate() { if (uniqueInstance == null) { uniqueInstance = new MyStartupActivity(); } return uniqueInstance; } </code></pre>
 

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