-4

How can I use findViewById() in a non activity class. Below is my code snippet. I get the error message: "can't resolve method findViewById" if used directly. And if i try to use the class constructor (Where the imageView is available) i get this error "cannot resolve symbol context"

public class MyBroadcastReceiver extends FirstBroadcastReceiver {

Activity activity;
public MyBroadcastReceiver(Context context, Activity activity){
    this.context=context;     // error here(cannot resolve symbol context)
    this.activity=activity;
}

    @Override
    protected void (Context context) {
    // content
    }

    @Override
    public void onButton(Context context, boolean isClick) {

        if(isClick) {

    ImageView blueImage = (ImageView)activity.findViewById(R.id.imageView);
    blueImage.setColorFilter(0xff000000);
         }
    }
.......
....
    // and so on

And below is my MainActivity with MybroadcastReceiver class instance.Is it correct?

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     // and so on
    }
}


MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver(MainActivity.this,this);

 @Override
 public void onActivityResult() {
  // some code
  }

@Override
     public void onInitialized(MyManager manager){
 // some code   
 }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
abdullah
  • 65
  • 10
  • What is your context parameter? If it is Activity, you can cast it to Activity and call findViewById, otherwise you can't – WenChao Jan 12 '17 at 23:06
  • Wait, what is `Receiver`? I'm guessing you mean `BroadcastReceiver`? – Michael Dodd Jan 12 '17 at 23:11
  • yes it is BroadcastReciever – abdullah Jan 13 '17 at 00:18
  • i also tried to make a function in main activity and use it in my broadcast but then it gives me this error "static method cannot be referenced from a static context" anyway around that? – abdullah Jan 13 '17 at 00:19
  • Tell me what exactly u trying to do with broadcast receiver coz I c u trying to set a button on it. What is it doing, tell in brief so any could be done – W4R10CK Jan 14 '17 at 02:55
  • I am extendng a broadcast receiver library from another project and using its function onclick to change the color of a imageview e.g from blue to black e.t.c...and this onclick is already defined in that library so just have to change the if statement. – abdullah Jan 14 '17 at 15:48

3 Answers3

0

A BroacastReceiver runs entirely in the background, listening for Intents sent either by the OS or other apps. It is not responsible for any UI interactions, and cannot access any views. Therefore, findViewById cannot be used within a BroadcastReceiver.

See also - What is BroadcastReceiver and when we use it?

Community
  • 1
  • 1
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • i also tried to make a function in main activity and use it in my broadcast but then it gives me this error "static method cannot be referenced from a static context" anyway around that? – abdullah Jan 13 '17 at 00:21
  • 1
    One thing to note: `BroadcastReceiver`s run on the main thread for your app, not on a background thread. – Karakuri Jan 13 '17 at 00:58
0

You have to pass View to the non activity class, before using findViewByid and try using

view.findViewByid(R.id.view_id);
Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
  • its not working error msg "cannot resolve symbol view" and as mentioned previously by other guysi cannot directly use findViewbyid in BroadcastReceiver. – abdullah Jan 13 '17 at 11:37
  • It will not work for Broadcast receiver, because broadcast Receiver can even work when view is null. You must change approach or try using callback mechanism – Milind Mevada Jan 13 '17 at 12:53
  • how do i use callback ...when i tried to make function in main activity and use it in my broadcast then it gives me this error "static method cannot be referenced from a static context" anyway around that – abdullah Jan 13 '17 at 13:08
0

Because context is null in Broadcast class. use Broadcast class constructor to pass parent_activity(Where the imageView is available) context in Broadcast to access the context:

public class Broadcast extends BroadCastReceiver {

Activity activity;
public Broadcast(Context context,Activity activity){
this.context=context;
this.activity=activity;
}
 .......
    ....... //so on

and in parent_activity create Broadcast class instance by passing parent_activity context as:

Broadcast broadcast = new Broadcast(parent_activity.this,this);

Use activity instance as:

@Override
public void onButton(Context context, boolean isClick) {

    if(isClick) {
       ImageView blueImage = (ImageView) activity.findViewById(R.id.imageView);  //<--- here
     }
}
 .........
      ......... //so on
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • I tried using your code but now my app crashes...i think its because of the class instance i am using from main activity? – abdullah Jan 13 '17 at 11:33
  • when i use this.context = context it gives me error 'cannot resolve symbol context' .. i will also upload the full code – abdullah Jan 13 '17 at 13:05