在Android应用程序中使用Intent显示另一个Activity

在我的Android应用程序中,我有两个Activity类。我在第一个按钮上有一个按钮,我想在点击第二个按钮时显示它,但我得到了一个错误。以下是课程:

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


});
}
}

第二个类应该在单击按钮时显示,但从未显示:

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


});
}
}

如何创建显示第二个活动的按钮?

429289 次浏览

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" />

Add this line to your AndroidManifest.xml:

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

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


});

----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>

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>

Intent i = new Intent("com.Android.SubActivity");
startActivity(i);

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


}
});
<activity android:name="[packagename optional].ActivityClassName"></activity>

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

add the activity in your manifest file

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

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