我怎么能叫Android的活动没有startAcitivity(意向)? [英] How can I call android Activity without startAcitivity(intent)?

查看:71
本文介绍了我怎么能叫Android的活动没有startAcitivity(意向)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序的主页我添加OptionsMenu。
当选择的选项联系我们被一个名为联系我们新的活动应该得到所谓的我想是。

In the main page of my android app I've added OptionsMenu. What i want is when the option "ContactUs" is selected a new Activity called "ContactUs" should get called.

public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (item.getTitle().toString().equals("ContactUs")) {
    ContactUs contact = new ContactUs(Home.this);
}
return true;
}

而联系我们的活动打开时,应显示其布局:

And when the ContactUs activity is opened its layout should be displayed:

public class ContactUs extends Activity{
private Context CONTEXT;

public CustomEqualizer(Context c){
    this.CONTEXT = c;
    setContentView(R.layout.contactus);
}
}

但问题是该布局将不显示直到onCreate方法被调用。
我的问题是我怎么能叫从家里没有startActivity联系我们的选择吗?

But the problem is the layout will not be displayed until onCreate method is called. My question is how can I call ContactUs option from Home without startActivity?

推荐答案

startActivity是启动和活动的唯一途径。您遇到的问题是,你要调用的setContentView()的,因为它没有得到应用程序上下文但它无论如何都不会工作的构造。你重写onCreate()方法,并从那里调用它。

startActivity is the only way to start and Activity. The problem you're having is that you're trying to call setContentView() in the constructor which won't work anyway since it hasn't gotten the application context yet. You override the onCreate() method and call it from there.

- = =更新 -

-= update =-

您可以将它添加到意图的传递捆绑任何你想要的活动之间。

You can pass whatever you want between activities by adding it to the Intent's bundle.

这篇关于我怎么能叫Android的活动没有startAcitivity(意向)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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