Using Intent in an Android application to show another activity


Question

In my Android application, I have two activity classes. I have a button on the first one and I want to show the second when it is clicked, but I get an error. Here are the classes:

public class FirstActivity extends Activity {

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

    Button orderButton = (Button)findViewById(R.id.order);

    orderButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View view) {
        Intent intent = new Intent(FirstActivity.this, OrderScreen.class);
        startActivity(intent);
      }

    });
  }
}

The second class that should show when the button is clicked, but never does:

public class OrderScreen extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.order);

    Button orderButton = (Button) findViewById(R.id.end);

    orderButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View view) {
        finish();
      }

    });
  }
}

How do I create a button that will show the second activity?

1
194
7/2/2012 3:58:58 AM

Accepted Answer

The issue was the OrderScreen Activity wasn't added to the AndroidManifest.xml. Once I added that as an application node, it worked properly.

<activity android:name=".OrderScreen" />
176
7/5/2015 5:06:59 AM

Add this line to your AndroidManifest.xml:

<activity android:name=".OrderScreen" /> 

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon