问题
i'm new to programming in Java and android... Here is my question, again not sure if the information I provide is sufficient. What I'm trying to do is create a simple two activity app.... So i have the main activity and a user clicks a button and a new activity is launched that sets a new layout.
I've looked at the two following websites:
http://developer.android.com/guide/components/fragments.html
http://www.vogella.com/articles/Android/article.html#fragments_tutorial
Both very useful but when I tried to implement had issues.
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button buttonLoadProfile = (Button) findViewById(R.id.buttonLoadProfile);
buttonLoadProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent profileIntent = new Intent();
profileIntent.setClass(getActivity(),LoadProfile.class);
// setContentView(R.layout.profile_layout);
}
});
}
The error I get is "The method getActivity() is undefined for the type new View.OnClickListener(){}"
回答1:
Use
Intent profileIntent = new Intent( MainActivity.this, LoadProfile.class );
startActivity(profileIntent);
This will resolve the enclosing class from the inner.
回答2:
You cannot call getActivity()
in the new View.OnClickListener()
interface because it does not have the method described in it.
Instead do the following:
Intent profileIntent = new Intent(this, LoadProfile.class);
And add:
startActivity(profileIntent);
To summarize the change:
public void onClick(View v) {
Intent profileIntent = new Intent(this, LoadProfile.class);
startActivity(profileIntent);
}
来源:https://stackoverflow.com/questions/13003898/launching-a-new-activity-android