Android的另一个活动的执行方法 [英] android execute method from another activity

查看:372
本文介绍了Android的另一个活动的执行方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从activityA呼吁是在activityB的方法。
方法是从所谓的正确B,而当我执行它里面webviewB.loadUrl(myUrl),应用程序崩溃的一个NullPointerException异常。同样webviewB方法,如果从activityB执行,工作正常。大概做是因为背景下,从A到B时调用必须设置,但如何?这是做的最好方法是什么?谢谢你。

ActivityA(tabHost)和ActivityB(tabcontent)正确初始化和ActivityB web视图显示URL1。
然后,当从ActivityA EVET是recived,我需要从ActivityB上的WebView加载URL2。

编辑:
当收到ActivityA事件,

  ActivityB测试=新ActivityB();
 test.recalcula();

ActivityB,

  @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        的setContentView(R.layout.vistaaib);        webviewB =(的WebView)findViewById(R.id.webviewB);
            webviewB.loadUrl(URL1);...
公共无效recalcula(){
        webviewB.loadUrl(URL2);}


解决方案

由Ortisi建议要么意图。或者,你为什么不尝试创建一个类,这个类创建方法,并调用该方法从两个活动。

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

}

解决方案

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.

这篇关于Android的另一个活动的执行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆