如何在Android 2.1中启动活动

问题描述:

我是使用Java Android进行编程的初学者。我目前对如何在Android 2.1中开展活动感到困惑。我目前的项目需要很多不同的活动才能在一个程序中一起工作。比方说,我有main.xml中内的按钮,并假设在函数内部ButtonAdroid.class低于一个:如何在Android 2.1中启动活动

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

我的目标是让到另一大类ButtonAndroid.class之间的连接,假设其名称是NextPage.java。你们知道我需要在public void onClick(View v)里面放置什么样的命令才能使当前活动切换到NextPage.java?


使用你的答案后,显然还是有错误。我有两个名为HelloRelativeLayout和HelloRelativeLayout2的类。

错误表示应用程序意外停止。这是否意味着我必须在XML中添加intent-filter或其他东西?

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

做,如果你通过即使1或2的Android开发教程去,你会知道如何做到这一点。 – Falmarri 2010-09-15 18:37:18

如果我正确认识你,你要移动到显示了不同的看法的另一项活动,则需要使用意向要做到这一点:

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

试试这个

您需要在清单文件中添加该类的活动

活动android:name =“。HelloRelativeLayout2在第一个活动下

我希望这是很有帮助的

您可以通过意向

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