I work in Eclipse. In general, there is a switch between the Activity by pressing the button, the necessary classes are created and everything else, a manifest is registered, as a result, we have Activation 1 with two buttons on Activation 2 and Activation 3 ... but when you click on any, it throws on Activation 2 (SecondActivity). Regardless of pressing button 1 or button 2, it opens the same window (SecondActivity). here is the code MainActivity.java

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button swith = (Button)findViewById(R.id.button1); Button swith3 = (Button)findViewById(R.id.button3); swith.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent SecAct = new Intent(getApplicationContext(), SecondActivity.class); Intent SecAct2 = new Intent(getApplicationContext(), SecondActivity2.class); startActivity(SecAct); startActivity(SecAct2); } }); swith3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent threActivity = new Intent(MainActivity.this, SecondActivity2.class); startActivity(threActivity); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

manifesto:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ru.example.carcar" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="SecondActivity" android:label="@string/app_name"/> <activity android:name="SecondActivity2" android:label="@string/app_name"/> </application> </manifest> 

Early z was delighted ((eclips does not issue errors, but the phone still has two buttons that open the same activation window ((... here again, can I copy something wrong?

public class MainActivity extends Activity {

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button swith = (Button)findViewById(R.id.button1); Button swith3 = (Button)findViewById(R.id.button3); swith.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, SecondActivity.class)); } }); swith3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, SecondActivity2.class)); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

p \ s swears at newnew syntactic obesity expected therefore wrote new

  • 2
    Well, you look at your code and think about how two consecutive startActivity(SecAct) and startActivity(SecAct2) can run SecAct ? - temq
  • Duplicate this question - pavlofff

1 answer 1

You need to specify only one intent per button. You had the last of the two, which switches to the same activity as the other button .... Here is the correct code

  switch.setOnClickListener(newnew OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, SecondActivity.class)); } }); swith3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, SecondActivity2.class)); } }); } 
  • swears at the newnew syntactic life expectancy so he wrote new and not newnew did not issue errors, but on the phone, while checking, it still opens the same activation - aivengos
  • 2
    @aivengos This code is correct. If it does not work for you, then the problem is not in this code, but in the buttons to which you bind listeners, or in the activation classes themselves that you call. It is also worth noting that before writing code, it is best to learn the language in which you write this code, programming with a spear method will not lead you to anything good - newnew is just a typo, there is no such operator in Java, such a thing should not cause you any difficulty and even more so be solved by some kind of "therefore wrote" - pavlofff