eclipse 如何导航到父活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18610722/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:09:05  来源:igfitidea点击:

How to navigate to a parent activity

androideclipseandroid-activity

提问by Yamen Nassif

Well when im working on something and i need to configure the action bar in my app i started from the http://developer.android.comand i found what i am looking for

好吧,当我在做某事并且我需要在我的应用程序中配置操作栏时,我从http://developer.android.com开始 ,我找到了我要找的东西

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
    NavUtils.navigateUpFromSameTask(this);
    return true;
}
return super.onOptionsItemSelected(item);}

ofcourse after adding the

当然在添加之后

<activity
    android:name="com.example.myfirstapp.DisplayMessageActivity"
    android:label="@string/title_activity_display_message"
    android:parentActivityName="com.example.myfirstapp.MainActivity" >
    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myfirstapp.MainActivity" />
</activity>

and the

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

i did all of this but when on my program i press the up button in the action bar the program crashes and here is the logcat

我做了所有这些,但是当我在我的程序中按下操作栏中的向上按钮时,程序崩溃了,这是 logcat

09-04 12:54:02.087: E/AndroidRuntime(11033): FATAL EXCEPTION: main
09-04 12:54:02.087: E/AndroidRuntime(11033): java.lang.IllegalArgumentException: Activity LegendActivity does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data>  element in your manifest?)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.support.v4.app.NavUtils.navigateUpFromSameTask(NavUtils.java:177)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at com.yay.android.projects.stories.LegendActivity.onOptionsItemSelected(LegendActivity.java:44)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.app.Activity.onMenuItemSelected(Activity.java:2611)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at com.android.internal.widget.ActionBarView.onClick(ActionBarView.java:206)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.view.View.performClick(View.java:4261)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.view.View$PerformClick.run(View.java:17356)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.os.Handler.handleCallback(Handler.java:615)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.os.Looper.loop(Looper.java:137)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at android.app.ActivityThread.main(ActivityThread.java:4921)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at java.lang.reflect.Method.invokeNative(Native Method)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at java.lang.reflect.Method.invoke(Method.java:511)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
09-04 12:54:02.087: E/AndroidRuntime(11033):    at dalvik.system.NativeStart.main(Native Method)

everything is just like they said ofcourse i have changed the activity names corresponding to names i have what's the problem here?

一切都和他们说的一样,当然我已经更改了与名称相对应的活动名称 我有什么问题?

回答by Muhannad A.Alhariri

You have to set the parent activity inside the Maniifest file

您必须在清单文件中设置父活动

<activity
            android:name="com.example.MainActivity"
            android:label="@string/title_activity_main"
            android:parentActivityName="com.example.MainUIActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.SampleActivity" />
        </activity>

Or use the Support-V4 as follows

或者使用 Support-V4 如下

NavUtils.navigateUpTo(sourceActivity, upIntent);

Or try the solution mentioned in this post

或者试试这篇文章中提到的解决方案

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        Intent upIntent = new Intent(this, YourListActivity.class);
        if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
            // This activity is not part of the application's task, so
            // create a new task
            // with a synthesized back stack.
            TaskStackBuilder
                    .from(this)
                    .addNextIntent(new Intent(this, HomeActivity.class))
                    .addNextIntent(upIntent).startActivities();
            finish();
        } else {
            // This activity is part of the application's task, so simply
            // navigate up to the hierarchical parent activity.
            NavUtils.navigateUpTo(this, upIntent);
        }
        return true;
    }
}

This is the best way to do so with android less than API level 11

这是使用低于 API 级别 11 的 android 执行此操作的最佳方法