How to start activity in Android 2.1

巧了我就是萌 提交于 2020-01-16 02:51:12

问题


I am a beginner in programming with Java Android. I am currently confused with how to start an activity in Android 2.1. My current project requires a lot of different activities to work together in one program. Let's say I have a button inside the main.xml and assume the function inside ButtonAdroid.class is the one below:

public class ButtonAndroid extends Activity {

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


         final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });
    }
}

My goal is to make a connection between the ButtonAndroid.class to another class, let's say its name is NextPage.java. Do you guys know what kind of commands do I need to put inside public void onClick(View v) that will enable the current activity to switch to NextPage.java?


After using your answer, apparently there is still an error. I have 2 classes named HelloRelativeLayout and HelloRelativeLayout2.

The error says the application has stopped unexpectedly. Does this mean I have to add intent-filter or something inside the XML?

public class HelloRelativeLayout extends Activity {

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

        final Button button = (Button) findViewById(R.id.signIn);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
             Intent i = new Intent(HelloRelativeLayout.this, HelloRelativeLayout2.class);
             startActivity(i);
            }
        });
    }
}

回答1:


If I'm understanding you correctly and you want to move on to another activity that displays a different view, you need to do this by using Intent:

Intent i = new Intent(ButtonAndroid.this, NextPage.class);
startActivity(i);



回答2:


Try this

You need to add the activity of the class in manifest file

activity android:name=".HelloRelativeLayout2 under the first activity

I hope this helpful




回答3:


You can do via intent

    //Start Activity
    Intent activityIntent = new Intent(context,GetLocation.class);
    activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(activityIntent);


来源:https://stackoverflow.com/questions/3720581/how-to-start-activity-in-android-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!