访问活动从其他活动类是如何看法 [英] how access activity view from other activity class

查看:102
本文介绍了访问活动从其他活动类是如何看法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,当我改变一个TextView Main.class它返回NullPointerException异常的文本。

my problem is when i change the text of a TextView Main.class it return nullPointerException.

这是我的code:

main.xml中

<TabHost ...>
  <RelativeLayout ...>
    <LinearLayout ...>
      <TabWidget .../>
       <FrameLayout ...>
        <ListView ../>
       </FrameLayout>
    </LinearLayout>

    <LinearLayout ...>
      <TextView android:id="@+id/status" ... />

    </LinearLayout>

  </RelativeLayout>
</TabHost>

Main.class

Main.class

....
TabHost tabHost;
TabHost.TabSpec spec;
Intent intent;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

    //display implementation of TabHost
    ....
    //Contacts tab
    intent = new Intent().setClass(this, Contacts.class);
    spec = tabHost.newTabSpec("contacts").setIndicator("Contacts",   res.getDrawable(R.drawable.ic_tab_contacts)).setContent(intent);
    tabHost.addTab(spec);

}

然后类,将更新的状态的TextView 是这样的:

Then class that will update the status TextView is this:

Contacts.class

Contacts.class

public class Contacts extends ListActivity{
    public void onCreate(Bundle savedInstanceState){
       super.onCreate(savedInstanceState);

       //gather info to display in ListView
       ....

       updateStatus("New Status");
    }

    private void updateStatus(String status){
       TextView labelView = (TextView) findViewById(R.id.status);
       labelView.setText(status);
    }

}


labelView.setText(status); Error occurs here....

请帮助我。我找不到在网的任何解决方案。
我需要更新状态....

Pls help me. I cant find any solution in the net. I need to update the status....

我的继承人清单:

...
 <application ...>
   <activity android:name=".Main" ... >
     <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
   </activity>

   <activity android:name=".Contacts"  android:label="@string/app_name"></activity> 
    <activity android:name=".OtherClass"  android:label="@string/app_name"></activity> 
    <activity android:name=".OtherClass2"  android:label="@string/app_name"></activity>
 </application>
...

在前进,感谢您的帮助。

In advance, thanks for any help.

推荐答案

您看到的ID视图状态存在的主要活动范围内,而你正在努力寻找它从联系人的活动环境。这就是为什么它返回

You see the view with id status exists in the Main activity context, while you are trying to find it from the Contacts activity context. Thats why it is returning null.

试试这个,它应该解决您的问题:

Try this, it should solve your problem :

主要活动

public class Main extends Activity {

    public static String sStatus = "";

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

            //display implementation of TabHost
            ....

    }

    @Override
    protected void onResume() {
        TextView labelView = (TextView) findViewById(R.id.status);
        labelView.setText(status);
    }
}

联系人活动

public class Contacts extends ListActivity{
    public void onCreate(Bundle savedInstanceState){
       super.onCreate(savedInstanceState);

       //gather info to display in ListView
       ....

       updateStatus("New Status");
    }

    private void updateStatus(String status){
        Main.sStatus = status;
    }

}

这篇关于访问活动从其他活动类是如何看法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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