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?
In the Manifest
In the Java Code where you have to place intent code
Use this code:
context: refer to current activity context,
please make sure that you have added activity in android manifest file.
Following code for adding activity in android manifest file
----FirstActivity.java-----
---OrderScreen.java---
---AndroidManifest.xml----
add the activity in your manifest file
Simply adding the activity which we want to switch to should be placed in the manifest file
The issue was the OrderScreen
Activity
wasn't added to the AndroidManifest.xml. Once I added that as an application node, it worked properly.