构造函数意图未定义



我试图将设置添加到我的第一个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

最新更新