Shinao
Shinao

Reputation: 137

How to call an Activity method from another class?

I have a WebView in my activity, which have a JSInterface :

mWebView.addJavascriptInterface(new JSInterface(mWebView, this), "interfaceWebsite");

When I call a function in my interface, I would like to see some Views (Textview/Button) modified.

public void changeStep(int newStep){
                step = newStep;
                tvStep.setText("Etape "+step);
                step_info = getString(R.string.step3_info);

        }
    }

step_info works (my options menu change), it's just a string var, but not my TextView, it throw VM aborting.

I call the function this way (where addWebsiteActivity is "this" in the code above) :

addWebsiteActivity.changeStep(step);

Is this possible to do that in a proper way ?

Thanks


Only the original thread that created a view hierarchy can touch its views

Taht's why I can call changeStep from JSInterface but I can inside my activity class... How can I do that then ?


And the solution is...

runOnUiThread(new Runnable() {
             public void run() {

                 step = newStep;
                 tvStep.setText("Etape "+step);
                 step_info = getString(R.string.step3_info);



            }
        });

Like this it knows it's a code for the main thread. But it's strange Eclipse throw me VM aborting exept that the explicit error above...

Upvotes: 0

Views: 802

Answers (2)

amukhachov
amukhachov

Reputation: 5900

changeStep method must be called inside UI thread. You can achieved this with runOnUiThread:

addWebsiteActivity.runOnUiThread(new Runnable() {
    public void run() {
        changeStep(step);
    }
})

Upvotes: 2

Housefly
Housefly

Reputation: 4422

change step to newStep in your code

Upvotes: 0

Related Questions