我试图将设置添加到我的第一个Android应用程序中,但我的意图有问题,代码是:
if (item.getItemId() == R.id.action_settings) {
Intent intent1 = new Intent(this, Settings.class);
startActivity(intent1);
return true;
}
错误是:
The constructor Intent(NavigationDrawerFragment, Class<Settings>) is undefined
有人能帮忙吗?
这种形式的Intent
构造函数的第一个参数是Context
。您的NavigationDrawerFragment
不是Context
,而Activity
应该是。您可以将getActivity()
用于片段中的Context
:
Intent intent1 = new Intent(getActivity(), Settings.class);
尝试使用:
Intent intent1 = new Intent(getActivity(), Settings.class);
startActivity(intent1);
return true;
代替:
Intent intent1 = new Intent(this, Settings.class);
startActivity(intent1);
return true;
将this更改为getActivity()或YourActivity.this