195

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?

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Tai Squared
  • 12,273
  • 24
  • 72
  • 82
  • Do you get an error when you compile or run time? In either case, what is the error? – Quintin Robinson Apr 10 '09 at 03:12
  • 3
    This was a run time error. The emulator gave the generic "the application has stopped unexpectedly" error, but using the debugger, it showed a "android.content.ActivityNotFoundException: Unable to find explicit activity class {class name} have you declared this activity in your AndroidManifest.xml? – Tai Squared Apr 10 '09 at 15:24
  • 8
    It is a very common bug that people forget to add their activity into Manifest.xml but there should be way to enter it automatically. – AZ_ Nov 24 '10 at 10:26

11 Answers11

177

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" />
N J
  • 27,217
  • 13
  • 76
  • 96
Tai Squared
  • 12,273
  • 24
  • 72
  • 82
  • Can you explain why this is needed? – Louis Rhys Sep 16 '12 at 14:27
  • @LouisRhys all activities need to be declared in the manifest file. See `Description` section here: http://developer.android.com/guide/topics/manifest/activity-element.html – ataulm Dec 22 '12 at 17:01
162

Add this line to your AndroidManifest.xml:

<activity android:name=".OrderScreen" /> 
user106011
  • 1,993
  • 2
  • 13
  • 2
16

----FirstActivity.java-----

    package com.mindscripts.eid;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

public class FirstActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button orderButton = (Button) findViewById(R.id.order);
    orderButton.setOnClickListener(new OnClickListener() {

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

 }
}

---OrderScreen.java---

    package com.mindscripts.eid;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;



    public class OrderScreen extends Activity {
@Override



protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_class);
    Button orderButton = (Button) findViewById(R.id.end);
    orderButton.setOnClickListener(new OnClickListener() {

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

 }
}

---AndroidManifest.xml----

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.mindscripts.eid"
  android:versionCode="1"
  android:versionName="1.0">


<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".FirstActivity"
              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=".OrderScreen"></activity>
</application>

Name is Nilay
  • 2,743
  • 4
  • 35
  • 77
Sunil Chavan
  • 161
  • 1
  • 2
4

Use this code:

Intent intent=new Intent(context,SecondActivty.class);
startActivity(intent);
finish();

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

<Activity name=".SecondActivity">
</Activity>
Mahesh
  • 2,862
  • 2
  • 31
  • 41
4
<activity android:name="[packagename optional].ActivityClassName"></activity>

Simply adding the activity which we want to switch to should be placed in the manifest file

jbihan
  • 3,053
  • 2
  • 24
  • 34
java dev
  • 1,044
  • 1
  • 11
  • 17
3

When you create any activity in android file you have to specify it in AndroidManifest.xml like

<uses-sdk android:minSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MyCreativityActivity"
        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=".OrderScreen"></activity>


</application>

3
b1 = (Button) findViewById(R.id.click_me);
        b1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(i);

            }
        });
Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
2

add the activity in your manifest file

<activity android:name=".OrderScreen" />
Neal Ahluvalia
  • 1,538
  • 1
  • 10
  • 20
2

In the Manifest

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

In the Java Code where you have to place intent code

startActivity(new Intent(CurrentActivity.this, OrderScreen.class);
Nilesh
  • 1,013
  • 14
  • 21
  • 1
    You are missing one closing bracket in `startActivity` It should be like this: `startActivity(new Intent(CurrentActivity.this, OrderScreen.class));` – Strange Feb 05 '19 at 10:40
1
Intent i = new Intent("com.Android.SubActivity");
startActivity(i);
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
Ndupza
  • 780
  • 1
  • 6
  • 16
1

you can use the context of the view that did the calling. Example:

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

orderButton.setOnClickListener(new View.OnClickListener() {

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

});
Bruno
  • 27
  • 1