This is what i want to do: I have a website with some buttons. The website is connected to my android app (via spacebrew). Depending on what button i click the background of an ImageButton changes. But every time i click on a button "setBackground" throws above exception.
This is my code:
public class MainActivity extends Activity{
ImageButton display;
SpacebrewClient client;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
...
//calls the method "changeDisplay"
client.addSubscriber("changeDisplay", SpacebrewMessage.TYPE_STRING, "changeDisplay");
}
public void changeDisplay(String input){
if(input.equals("topay")){
display = (ImageButton)findViewById(R.id.imageButton1);
display.setBackground(getResources().getDrawable(R.drawable.display_2));
}
...
}
}
I found this possible solution: first answer. But that doesn't seem to work for me. I still get the same exception.
Edit: Tried the second solution, as well. Now "setBackground" throws a NullPointerException.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
display = (ImageButton)findViewById(R.id.imageButton1);
}
public void changeDisplay(String input){
if(input.equals("topay")){
runOnUiThread(new Runnable() {
public void run() {
display.setBackground(getResources().getDrawable(R.drawable.display_2));
}
});}