Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

java - android execute method from another activity

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);

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...