-1

I am calling from activityA a method that is on activityB. Method is called from A to B properly but when I execute inside it webviewB.loadUrl(myUrl), app crashes for a nullpointerexception. Same webviewB method, if executed from activityB, works properly. Probably is done because context, when calling from A to B must be set, but how? Which is the best way to do it? Thank you.

ActivityA (tabHost) and ActivityB (tabcontent) are initialized properly and webview from ActivityB shows URL1. Then when evet from ActivityA is recived, I need to load URL2 on webview from ActivityB.

EDIT: when event is received on ActivityA,

 ActivityB test = new ActivityB();
 test.recalcula();

ActivityB,

@Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setContentView(R.layout.vistaaib); 

        webviewB = (WebView) findViewById(R.id.webviewB);


            webviewB.loadUrl(URL1);

...


public void recalcula (){


        webviewB.loadUrl(URL2);

}
Jaume
  • 3,672
  • 19
  • 60
  • 119
  • 1
    here webviewB is null..as webviewB is in layoutB xml and it is initialized/inflated in onCreate(). So you are not able to do this...try some different approche – Mohsin Naeem Jul 06 '12 at 16:09
  • I know that I am not able! that is why am asking for "different approche" – Jaume Jul 06 '12 at 16:20
  • why you need to load the webview of ActivityB??..why not just put that webview in ActivityA – Mohsin Naeem Jul 06 '12 at 16:29
  • due to multiple reasons I am not able to set webview on same tabhost activity :( – Jaume Jul 06 '12 at 17:19
  • so if in case you are able to achieve this..then you want UI of `ActivityA` is invisible? and the webview become visible? – Mohsin Naeem Jul 06 '12 at 17:25

2 Answers2

0

The best way to communicate between activities is using the Intent mechanism. So just launch an intent from activityA in order to communicate to activityB that its loadUrl method should run.

Or for other solutions you can read this question: Best way to accomplish inter-activity communication in an Android TabHost application

Community
  • 1
  • 1
Stefano Ortisi
  • 5,288
  • 3
  • 33
  • 41
0

Either intents as suggested by Ortisi. Or Why don't you try creating a class, create that method in this class, And call that method from both the activity.

AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • Sorry but I do not see any difference on what I am doing now. Could you please explain it better? – Jaume Jul 06 '12 at 16:19
  • Yes why not, I would love to post a detailed approach. But for that Please include your method code, and need of that in your question. :) – AAnkit Jul 06 '12 at 16:33
  • I tried to be specific and clear. Please see my edits. I appreciate so much your help – Jaume Jul 06 '12 at 17:05
  • your webView is NULL at the time u call this method so the exception is justified. I am not getting Why it is require to call and load webview which is yet to be defined – AAnkit Jul 06 '12 at 17:12
  • for some any other reasons I must do it like this. When event is received on ActivityA, webviewB is reloaded... – Jaume Jul 06 '12 at 17:16
  • Then why dont you just startActivityB, it will automatically reloaded. ?? – AAnkit Jul 06 '12 at 17:19
  • because of its loading time! works but that solution is what I am trying to avoid – Jaume Jul 06 '12 at 17:20
  • I have on doubt. You are getting Events in Activity A and calling Method of Activity B. Which Activity Is in Front/visible this time. If activity B is visible!! then You must call startActivity(B), Although if you don't want to create multiple instance of Activity B you can use launch mode android:launchMode="singleInstance". But i dont think there could be a way to avoid start activity. – AAnkit Jul 06 '12 at 17:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13524/discussion-between-ankit-and-jaume) – AAnkit Jul 06 '12 at 17:35