6

这是与主题相关的清单代码:

   <activity
        android:name="smartHomes.capstone.GeneralAndSecurity"
        android:label="@string/title_activity_general_and_security"
        android:parentActivityName="smartHomes.capstone.HomePage" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="smartHomes.capstone.HomePage" />
    </activity>

这是错误日志:

   03-20 21:19:18.227: E/AndroidRuntime(787): FATAL EXCEPTION: main
   03-20 21:19:18.227: E/AndroidRuntime(787): java.lang.IllegalArgumentException: Activity           GeneralAndSecurity does not have a parent activity name specified. (Did you forget to add the  android.support.PARENT_ACTIVITY <meta-data>  element in your manifest?)
   03-20 21:19:18.227: E/AndroidRuntime(787):   at android.support.v4.app.NavUtils.navigateUpFromSameTask(NavUtils.java:177)
   03-20 21:19:18.227: E/AndroidRuntime(787):   at smartHomes.capstone.GeneralAndSecurity.onOptionsItemSelected(GeneralAndSecurity.java:41)
   03-20 21:19:18.227: E/AndroidRuntime(787):   at com.actionbarsherlock.app.SherlockActivity.onMenuItemSelected(SherlockActivity.java:208)

请问我能知道为什么返回按钮返回错误吗?

4

3 回答 3

2

同样对我有用meta-data

这是我的例子。

    <activity android:name="com.redplanet.sandboxandroid.ui.MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.redplanet.sandboxandroid.ui.SecondActivity"
        android:parentActivityName="com.redplanet.sandboxandroid.ui.MainActivity" >
    </activity>

我的SecondActivity课。

public class SecondActivity extends SherlockFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.countries_list);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            break;
        }
        return super.onOptionsItemSelected(item);

    }

}
于 2013-06-06T11:45:35.113 回答
2

我遇到了同样的问题,但这只是包名中的拼写错误。您是否仔细检查了包和类名?

于 2014-02-12T12:32:35.200 回答
0

尝试在您的清单中将其添加到您的父活动中:

android:allowBackup="true"

例子:

<activity
    android:name=".View.MainActivity"
    android:allowBackup="true">
</activity>

<activity 
    android:name=".View.ChildActivity"
    android:parentActivityName=".View.ChildActivity">
    <meta-data
         android:name="android.support.PARENT_ACTIVITY"
         android:value=".View.MainActivity" />
</activity>

在您的 ChildActivity 中:

public class ChildActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
于 2017-02-27T14:48:03.493 回答