首页 文章

意图不在NavigationDrawerFragment中工作

提问于
浏览
1

我正在尝试使用NavigationDrawerFragment调用另一个活动 . 在我的onOptionsItemSelected下,我创建一个intent并调用所述intent但由于某种原因它显示为错误 .

Intent intent = new Intent(this, HomeActivity.class);

我想知道为什么它从那时起不起作用:

  • 我已经有了一个HomeActivity类

  • 我也在清单中添加了它

  • 其他活动也在调用HomeActivity类,它似乎工作正常 .

@Override public boolean onOptionsItemSelected(MenuItem item){if(mDrawerToggle.onOptionsItemSelected(item)){return true; }

if (item.getItemId() == R.id.action_example) {
        Toast.makeText(getActivity(), "Example action.", Toast.LENGTH_SHORT)
                .show();


       int pos = 1;
       switch(pos){

       case 1:
           Intent intent = new Intent(this, HomeActivity.class);
            this.startActivity(intent);
            break;  

       }

        return true;
    }

    return super.onOptionsItemSelected(item);
}

我在代码中做错了什么,这就是为什么它显示为错误?更新:

错误消息是这样的:

构造函数Intent(NavigationDrawerFragment,Class)未定义

1 回答

  • 1

    如果此代码段来自 Fragment ,则应使用 new Intent(getActivity(), HomeActivity.class); .

    顺便说一句,这同样适用于以下行 .

相关问题