ActionBarSherlock:OnOptionsItemSelected无法识别R.id.home

bug*_*oid 10 android android-activity actionbarsherlock

我正在使用ActionBarSherlock库,我按照此处此处建议的确切步骤启用导航到上一个屏幕.

我的代码看起来像这样:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // This callback is used only when mSoloFragment == true (see
    // onActivityCreated above)
    switch (item.getItemId()) {
    case android.R.id.home:
        // App icon in Action Bar clicked; go up
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Reuse the
                                                            // existing
                                                            // instance
        startActivity(intent);

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
Run Code Online (Sandbox Code Playgroud)

但是R.id.home没有得到承认,家里出现了红色.: - /如果我使用本机操作栏,则home声明将我带到ids.xml文件.但是在这里使用ActionBarSherlock Activity时找不到声明.我错过了什么吗?

Dha*_*mar 12

只需要替换它

android.R.id.home
Run Code Online (Sandbox Code Playgroud)

R.id.home
Run Code Online (Sandbox Code Playgroud)

并检查你的代码...运行它

因为

R.layout.* 是您提供的布局(例如,在res/layout中).

android.R.layout.* 是Android SDK附带的布局.


Fra*_*Rem 7

我知道这是一个老问题,但我相信没有正确的答案.

应该是android.R.id.home因为它是一个平台资源,所以你的代码很好.

确保自home11年推出以来,你的minSdkVersion为11或更高.