如何在 Android Eclipse 上的 Manifest.xml 中声明多个活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10680465/
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
How to declare multiple activities in Manifest.xml on Android Eclipse
提问by Rohith
I am self learner to android.
Right now i have five pages in an android application.The first page contains two buttons(button1,button2).If i trigger the button1 it should go to the second page,which contains one button,again if i trigger this button finally it should simply display a tab host which laying on the third page.Likewise if i trigger the button2 from the page one it should go to page four and page five as like what the button1 has done.
我是 android 的自学者。
现在我在一个 android 应用程序中有五个页面。第一页包含两个按钮(button1,button2)。如果我触发 button1,它应该转到包含一个按钮的第二页,如果我最终触发这个按钮,它应该只需显示一个位于第三页上的标签主机。同样,如果我从第一页触发 button2,它应该像 button1 所做的那样转到第四页和第五页。
During at run when i am clicking the button from page four,it shown error message as
Sorry! The application Tabhost_demo has stopped unexpectedly.please try again!
(Note : Other Pages activities are working well rather than four and five).
在运行期间,当我单击第四页的按钮时,它显示错误消息为
抱歉!Tabhost_demo 应用程序意外停止。请重试!
(注意:其他页面活动运行良好,而不是四个和五个)。
Refer java class source
参考java类源
public class First_index extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 = (Button) findViewById(R.id.button_parent);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Index_parent.class);
startActivityForResult(myIntent, 0);
}
});
This is for page switching right?
这是用来切换页面的吧?
AND THIS SOURCE IS FOR TABHOSTING
这个来源是为TABHosting
public class Parent_concpt extends TabActivity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
TabHost tabHost = getTabHost();
// Tab for Attendance
TabSpec pho = tabHost.newTabSpec("Attendance");
// setting Title and Icon for the Tab
pho.setIndicator("Attendance",getResources().getDrawable(R.drawable.icon_att_parent));
Intent phoIntent = new Intent(this, Att_parents.class);
pho.setContent(phoIntent);
// Tab for Marks
TabSpec mrk = tabHost.newTabSpec("Achievement");
mrk.setIndicator("Achievement", getResources().getDrawable(R.drawable.icon_achieve_parents));
Intent mrkIntent = new Intent(this, Achieve_parents.class);
mrk.setContent(mrkIntent);
// Tab for Fee
TabSpec fees = tabHost.newTabSpec("Fee details");
fees.setIndicator("Fee details",getResources().getDrawable(R.drawable.icon_fee_details));
Intent feesIntent = new Intent(this, Feedet_parents.class);
fees.setContent(feesIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(pho);
tabHost.addTab(mrk);
tabHost.addTab(fees);}}
My ambigution is,i guess only the problem is on Manifest.xml file.IS THERE ANY PERMISSIONS AVAILABLE TO MENTION FOR EVERY ACTIVITY?
我的歧义是,我想只有 Manifest.xml 文件有问题。是否有任何权限可以提及每个活动?
Refer Manifest.xml source
参考 Manifest.xml 源码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.demo.student"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity android:name=".First_index"
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=".Index_parent"></activity>
<activity android:name=".Index_parent"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN1" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Parent_concpt"></activity>
<activity android:name=".Parent_concept"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN3" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Att_parents -->
<activity android:name=".Att_parents" />
<!-- Achieve_parents -->
<activity android:name=".Achieve_parents" />
<!-- Feedet_parents -->
<activity android:name=".Feedet_parents" />
<activity android:name=".Index_principal"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN2" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity><activity android:name=".Princy_concept"></activity>
<activity android:name=".Princy_concept"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN7" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Att_principal Activity -->
<activity android:name=".Att_principal" />
<!-- Perform_principal Activity -->
<activity android:name=".Perform_principal" />
<!-- Coll_principal Activity -->
<activity android:name=".Coll_principal" />
<!-- Xpense_principal Activity -->
<activity android:name=".Xpense_principal" />
</application>
</manifest>
NOTE :I have perfectly mentioned each and every class and layout's file name perfectly.
注意:我已经完美地提到了每个类和布局的文件名。
Please refer my logcat here LOGCAT
请在此处参考我的 logcat LOGCAT
05-21 12:13:16.013: W/KeyCharacterMap(274): No keyboard for id 0
05-21 12:13:16.013: W/KeyCharacterMap(274): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-21 12:13:19.843: D/AndroidRuntime(274): Shutting down VM
05-21 12:13:19.843: W/dalvikvm(274): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-21 12:13:19.853: E/AndroidRuntime(274): FATAL EXCEPTION: main
05-21 12:13:19.853: E/AndroidRuntime(274): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.demo.student/com.android.demo.student.Princy_concept}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
05-21 12:13:19.853: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-21 12:13:19.853: E/AndroidRuntime(274): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-21 12:13:19.853: E/AndroidRuntime(274): at android.app.ActivityThread.access00(ActivityThread.java:125)
05-21 12:13:19.853: E/AndroidRuntime(274): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-21 12:13:19.853: E/AndroidRuntime(274): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 12:13:19.853: E/AndroidRuntime(274): at android.os.Looper.loop(Looper.java:123)
05-21 12:13:19.853: E/AndroidRuntime(274): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-21 12:13:19.853: E/AndroidRuntime(274): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 12:13:19.853: E/AndroidRuntime(274): at java.lang.reflect.Method.invoke(Method.java:521)
05-21 12:13:19.853: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-21 12:13:19.853: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-21 12:13:19.853: E/AndroidRuntime(274): at dalvik.system.NativeStart.main(Native Method)
05-21 12:13:19.853: E/AndroidRuntime(274): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
05-21 12:13:19.853: E/AndroidRuntime(274): at android.app.TabActivity.onContentChanged(TabActivity.java:105)
05-21 12:13:19.853: E/AndroidRuntime(274): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
05-21 12:13:19.853: E/AndroidRuntime(274): at android.app.Activity.setContentView(Activity.java:1647)
05-21 12:13:19.853: E/AndroidRuntime(274): at com.android.demo.student.Princy_concept.onCreate(Princy_concept.java:16)
05-21 12:13:19.853: E/AndroidRuntime(274): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-21 12:13:19.853: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-21 12:13:19.853: E/AndroidRuntime(274): ... 11 more
Thanks for your help!
谢谢你的帮助!
采纳答案by antew
It is telling you that in your layout main3
, you must have a TabHost
named android.R.id.tabhost
它告诉你,在你的布局中main3
,你必须有一个TabHost
命名的android.R.id.tabhost
In onCreate()
you are inflating main3:
在onCreate()
你膨胀 main3 中:
setContentView(R.layout.main3);
Open up main3.xml and make sure your layout looks something like this:
打开 main3.xml 并确保您的布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
Note the TabHost
's name and the name of the TabWidget
.
请注意TabHost
的名称和 的名称TabWidget
。
See herefor a full example.
有关完整示例,请参见此处。
回答by Abdulla Shoshaa
It's to late to answer this but I guess it may help others.
回答这个为时已晚,但我想它可能会帮助其他人。
In your manifest.xml file you need to specify only one Activity as Luncher Activity which is going to pop up when the app starts.
在 manifest.xml 文件中,您只需将一个活动指定为午餐活动,该活动将在应用启动时弹出。
assigning more than one Activity to be Luncher will trigger java.lang.RuntimeException: Unable to start activity
Exception.
将多个 Activity 分配为 Luncher 将触发java.lang.RuntimeException: Unable to start activity
异常。
so the right way to declare other Activities beside the luncher Activity is by changing its category to Default or other category.
所以在午餐者活动旁边声明其他活动的正确方法是将其类别更改为默认或其他类别。
to find more about Intent-filter Categories please go to: http://developer.android.com/guide/topics/manifest/category-element.html
要了解有关意图过滤器类别的更多信息,请访问:http: //developer.android.com/guide/topics/manifest/category-element.html
and for more readings on manifest.xml file: http://developer.android.com/guide/topics/manifest/manifest-intro.html
以及更多关于 manifest.xml 文件的阅读:http: //developer.android.com/guide/topics/manifest/manifest-intro.html
<intent-filter>
<action android:name="android.intent.action.YourActivityName" />
<category android:name="android.intent.category.Default" />
</intent-filter>