I have an issue, I need to access views in an activity from a service. I thought a good way to do that was to pass the activity context and then use it to access the views. Yet, when i run it, it throws a null error, meaning the object (the view) is null. This is the code, i would like to know how to fix this:
public class Purchase extends AppCompatActivity {
private TextView purchaseAmount;
private Button but;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_purchase);
but = (Button) findViewById(R.id.makePurBut);
purchaseAmount = (TextView) findViewById(R.id.purchaseAmout);
Intent i = new Intent(Purchase.this, MyHostApduService.class);
MyHostApduService myHostApduService = new MyHostApduService(Purchase.this);
startService(i);
}
}
public class MyHostApduService extends HostApduService {
static Context context;
public MyHostApduService(Context context)
{
this.context = context;
}
public int onStartCommand(Intent intent, int flags, int startId) {
textView = (TextView)((Activity)context).findViewById(R.id.purchaseAmout);
but = (Button)((Activity)context).findViewById(R.id.makePurBut);
return flags;
}
}