在 Android 应用程序中使用 Intent 显示另一个活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/736571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Intent in an Android application to show another activity
提问by Tai Squared
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:
在我的 Android 应用程序中,我有两个活动类。我在第一个按钮上有一个按钮,我想在单击它时显示第二个按钮,但出现错误。以下是课程:
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?
如何创建将显示第二个活动的按钮?
采纳答案by Tai Squared
The issue was the OrderScreenActivity
wasn't added to the AndroidManifest.xml. Once I added that as an application node, it worked properly.
问题是OrderScreenActivity
没有添加到AndroidManifest.xml 中。一旦我将其添加为应用程序节点,它就可以正常工作。
<activity android:name=".OrderScreen" />
回答by user106011
Add this line to your AndroidManifest.xml:
将此行添加到您的 AndroidManifest.xml 中:
<activity android:name=".OrderScreen" />
回答by Sunil Chavan
----FirstActivity.java-----
---- 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---
--- 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----
--- 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>
回答by Mahesh
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.
请确保您已在 android 清单文件中添加了活动。
Following code for adding activity in android manifest file
以下代码用于在 android 清单文件中添加活动
<Activity name=".SecondActivity">
</Activity>
回答by java dev
<activity android:name="[packagename optional].ActivityClassName"></activity>
Simply adding the activity which we want to switch to should be placed in the manifest file
简单地添加我们想要切换到的活动应该放在清单文件中
回答by Hiren Patel
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);
}
});
回答by Android-iPhone-rahul
When you create any activity in android file you have to specify it in AndroidManifest.xml like
当您在 android 文件中创建任何活动时,您必须在 AndroidManifest.xml 中指定它,例如
<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>
回答by Neal Ahluvalia
add the activity in your manifest file
在清单文件中添加活动
<activity android:name=".OrderScreen" />
回答by Nilesh Panchal
In the Manifest
在清单中
<activity android:name=".OrderScreen" />
In the Java Code where you have to place intent code
在必须放置意图代码的 Java 代码中
startActivity(new Intent(CurrentActivity.this, OrderScreen.class);
回答by Ndupza
Intent i = new Intent("com.Android.SubActivity");
startActivity(i);