Usar Intent en una aplicación Android para mostrar otra actividad


En mi aplicación Android, tengo dos clases de actividad. Tengo un botón en el primero y quiero mostrar el segundo cuando se hace clic, pero recibo un error. Aquí están las clases:

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);
      }

    });
  }
}

La segunda clase que debería mostrarse cuando se hace clic en el botón, pero nunca lo hace:

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();
      }

    });
  }
}

¿Cómo puedo crear un botón que muestre la segunda actividad?

Author: Alex Lockwood, 2009-04-10

11 answers

El problema fue el OrderScreen Activity no se ha añadido al AndroidManifest.xml . Una vez que agregué eso como un nodo de aplicación, funcionó correctamente.

<activity android:name=".OrderScreen" />
 175
Author: Tai Squared,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-05 05:06:59

Añade esta línea a tu AndroidManifest.xml:

<activity android:name=".OrderScreen" /> 
 161
Author: user106011,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-12 18:19:25

----Primera actividad.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>

 16
Author: Sunil Chavan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-14 07:26:53

Utilice este código:

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

Contexto: refiérase al contexto de actividad actual,

Por favor, asegúrese de que ha añadido actividad en el archivo de manifiesto de Android.

Siguiente código para agregar actividad en el archivo de manifiesto de Android

<Activity name=".SecondActivity">
</Activity>
 4
Author: Mahesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-20 19:04:56
<activity android:name="[packagename optional].ActivityClassName"></activity>

Simplemente agregar la actividad a la que queremos cambiar debe colocarse en el archivo de manifiesto

 4
Author: java dev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-11 01:39:58

Al crear cualquier actividad en un archivo de Android, debe especificarlo en AndroidManifest.xml como

<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
Author: Android-iPhone-rahul,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-06 10:24:38
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);

            }
        });
 3
Author: Hiren Patel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-02-09 08:34:26

Agregue la actividad en su archivo de manifiesto

<activity android:name=".OrderScreen" />
 2
Author: Neal Ahluvalia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-01 06:17:45

En el Manifiesto

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

En el Código Java donde tienes que colocar el código intent

startActivity(new Intent(CurrentActivity.this, OrderScreen.class);
 2
Author: Nilesh Panchal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-24 06:50:35

Puede usar el contexto de la vista que hizo la llamada. Ejemplo:

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);
  }

});
 1
Author: Bruno,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-13 13:21:20
Intent i = new Intent("com.Android.SubActivity");
startActivity(i);
 1
Author: Ndupza,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-15 17:43:39