问题描述
下面是我的code的一部分。我试图在其中当你点击第一个列表项它将启动活动 MrsClubb
导航菜单。然而,当我把这个在我的code将其与错误出现:
无法解析构造意愿(android.widget.AdapterView.OnItemClickListener,java.lang.Class中< COM ....等>)
任何想法如何解决这个问题?
双**显示了在code中的误差。
下面是code的部分:
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
工具条工具栏=(栏)findViewById(R.id.toolbar);
mDrawerLayout =(DrawerLayout)findViewById(R.id.drawer);
mDrawerList =(ListView控件)findViewById(android.R.id.list);
mDrawerListItems = getResources()getStringArray(R.array.drawer_list)。
mDrawerList.setAdapter(新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,mDrawerListItems));
mDrawerList.setOnItemClickListener(新AdapterView.OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
开关(位置){
情况下0:
意图I =新意图**(这一点,MrsClubb.class)**
startActivity(ⅰ);
}
mDrawerLayout.closeDrawer(mDrawerList); }
});
mDrawerToggle =新ActionBarDrawerToggle(这一点,
mDrawerLayout,
工具栏,
R.string.drawer_open,
R.string.drawer_close){
公共无效onDrawerClosed(视图v){
(五)super.onDrawerClosed;
invalidateOptionsMenu();
syncState();
}
公共无效onDrawerOpened(视图v){
(五)super.onDrawerOpened;
invalidateOptionsMenu();
syncState();
}
};
问题:
您不能使用这个
来指活动
的内部类中,如这个
变得对内部类的引用。在构造函数的含义不解决
消息是编译器间$ P $点为
意图(AdapterView.OnItemClickListener监听器,Class类)
,它不识别,而不是
意图(上下文的背景下,Class类)
这是正确的,而编译器所期待的。
解决方案:
替换
意向书I =新意图(这一点,MrsClubb.class);
与
意向书I =新意图(MyActivity.this,MrsClubb.class);
其中, MyActivity
是本code所属的活动
类的名称。
Here is a section of my code. I am trying to make a navigation menu in which when you click on the first list item it launches the activity MrsClubb
. However when I put this into my code it comes up with the error:
Cannot resolve constructor 'Intent(android.widget.AdapterView.OnItemClickListener,java.lang.Class<com....etc>)'
Any ideas how to resolve this?
The double ** shows where in the code the error is.
Here is the section of the code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer);
mDrawerList = (ListView)findViewById(android.R.id.list);
mDrawerListItems = getResources().getStringArray(R.array.drawer_list);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDrawerListItems));
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position) {
case 0:
Intent i = new Intent**(this, MrsClubb.class);**
startActivity(i);
}
mDrawerLayout.closeDrawer(mDrawerList);
}
});
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
toolbar,
R.string.drawer_open,
R.string.drawer_close){
public void onDrawerClosed(View v){
super.onDrawerClosed(v);
invalidateOptionsMenu();
syncState();
}
public void onDrawerOpened(View v){
super.onDrawerOpened(v);
invalidateOptionsMenu();
syncState();
}
};
The Problem:
You cannot use this
to refer to the Activity
inside an inner class, as this
becomes a reference to the inner class. The meaning of the constructor not resolved
message is that the compiler interprets it as
Intent(AdapterView.OnItemClickListener listener, Class class)
which it does not recognize, instead of
Intent(Context context, Class class)
which is correct and what the compiler expects.
The Solution:
Replace
Intent i = new Intent(this, MrsClubb.class);
with
Intent i = new Intent(MyActivity.this, MrsClubb.class);
where MyActivity
is the name of the Activity
class in which this code belongs.
这篇关于Android的无法解析构造意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!