2010-09-15 126 views
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

回答

3

如果我正確認識你,你要移動到顯示了不同的看法的另一項活動,則需要使用意向要做到這一點:

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

試試這個

您需要在清單文件中添加該類的活動

活動android:name =「。HelloRelativeLayout2在第一個活動下

我希望這是很有幫助的

0

您可以通過意向

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