android.R.id.home 找不到符号

moh*_*eri 5 android android-resources

我在 AppCompat 库中使用了 Toolbar 组件而不是 Default Actionbar。

编译时间:我得到无法找到符号 android.R.id.home 的编译错误

public void setupActionBar() {
    // Set a Toolbar to replace the ActionBar.
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if(id == R.id.action_help) {
        showHelp();
        return true;
    }else if(id == android.R.id.home){
        Log.d(TAG, "Back Button clicked!");
        this.finish();
        return true;
    }

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

小智 2

android.R.id.home 是在 API 级别 11 中引入的。这里有更多详细信息: /sf/answers/1310336331/