Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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?

See Question&Answers more detail:os

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

1 Answer

startActivity() is the only way to start an 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...