0

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));
            }
      });}
Community
  • 1
  • 1
mäx
  • 23
  • 3
  • You can only modify the UI in the UI thread. You can post a Runnable to the UI thread with `display. post(new Runnable() {...});`. – Xaver Kapeller Jun 28 '14 at 12:59
  • Still get a NullPointer. Maybe the problem's not "setBackground", but "findViewById"? – mäx Jun 28 '14 at 13:05
  • You should always perform all `findViewById()` in `onCreate()` and save the references to the `Views` as member variables. But that won't solve your problem, are you sure the id is correct? Does this layout have the `View` you want? – Xaver Kapeller Jun 28 '14 at 13:07
  • Is your `ImageButton` in the fragment layout? – Apoorv Jun 28 '14 at 13:07
  • go through this link .. http://stackoverflow.com/questions/10118301/android-viewrootimplcalledfromwrongthreadexception – Jogendra Gouda Jun 28 '14 at 13:08
  • Yes, there is a ImageButton with id "imageButton1" in my fragment_main.xml @Jogendra Gouda: That's the link i posted in my question. – mäx Jun 28 '14 at 13:13

1 Answers1

0

Ok, i managed to solve my problem. Here's what i did:

I created a Handler ...

Handler handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
          Bundle bundle = msg.getData();
          String input = bundle.getString("input");
          ImageButton display = (ImageButton)findViewById(R.id.imageButton1);
          if(input.equals("topay")){
              display.setBackground(getResources().getDrawable(R.drawable.display_2));
          } 
          else if ...
         }
     };

... and then a new Runnable, which passes the input of changeDisplay to the Handler.

Runnable runnable = new Runnable() {
            public void run() {         
                Message msg = handler.obtainMessage();
                Bundle bundle = new Bundle();
                String message = "";
                if(input.equals("topay")){
                    message = "topay";
                }
                else if ...
                bundle.putString("input", message);
                msg.setData(bundle);
                handler.sendMessage(msg);
            }
      };
      Thread mythread = new Thread(runnable);
         mythread.start();

Now it's working! :-)

But thanks for your help, anyway!

mäx
  • 23
  • 3