package com.sarham.kabs.fruity;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener{
private DrawerLayout drawerLayout;
private ListView listView;
private String[] planets;
private ActionBarDrawerToggle drawerListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
listView = (ListView)findViewById(R.id.drawerListView);
planets = getResources().getStringArray(R.array.planets);
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, planets));
listView.setOnItemClickListener(this);
drawerListener = new ActionBarDrawerToggle(this, drawerLayout, R.mipmap.ic_drawer, R.string.drawer_open, R.string.drawer_close){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
Toast.makeText(MainActivity.this,"Drawer Open", Toast.LENGTH_LONG).show();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
Toast.makeText(MainActivity.this, "Drawer Closed", Toast.LENGTH_LONG).show();
}
};
drawerLayout.setDrawerListener(drawerListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, planets[position]+" Was selected", Toast.LENGTH_LONG).show();
selectItem(position);
}
public void selectItem(int position){
listView.setItemChecked(position, true);
setTitle(planets[position]);
}
public void setTitle(String title){
getSupportActionBar().setTitle(title);
}
}
我在Android工作室的一个项目上工作,我试图放置导航抽屉图标但是我收到了这个错误:&#39;找不到符号R.mipmap.if_drawer&#39;, 我已经尝试将它放在drawable中,但是对于符号&#39; R.drawable,会抛出同样的错误。 ic_drawer&#39;
清洁和重新制作后,我收到以下消息:
错误:(32,26)错误:没有为ActionBarDrawerToggle找到合适的构造函数(MainActivity,DrawerLayout,int,int,int) 构造函数ActionBarDrawerToggle.ActionBarDrawerToggle(Activity,Toolbar,DrawerLayout,T,int,int)不适用 (无法从参数实例化,因为实际和形式参数列表的长度不同) 构造函数ActionBarDrawerToggle.ActionBarDrawerToggle(Activity,DrawerLayout,Toolbar,int,int)不适用 (实际的参数int不能通过方法调用转换转换为工具栏) 构造函数ActionBarDrawerToggle.ActionBarDrawerToggle(Activity,DrawerLayout,int,int)不适用 (实际和正式的参数列表长度不同) 其中T是一个类型变量: T扩展了Drawable,在构造函数ActionBarDrawerToggle(Activity,Toolbar,DrawerLayout,T,int,int)中声明的DrawerToggle
答案 0 :(得分:5)
请清洁您的项目&amp; 重建。
<强> Build > Clean Project Then Build > Rebuild Project
强>
答案 1 :(得分:1)
检查IDE投掷的错误及其提供的建议,以下是我学到的内容: ActionBarDrawerToggle有两个构造函数:
ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int drawerOpenContentDescription, int drawerClosedContentDescription)
ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, int drawerOpenContentDescription, int drawerClosedContentDescription)
我使用了带有4个参数的第二个构造函数,导航抽屉工作正常,没有放置'ic_drawer'图标,而是用后箭头替换。
答案 2 :(得分:0)
尝试rebuild
您的项目。如果你有正确的名字,请仔细检查。大写字母也会计入名称。那个文件的扩展名是什么?
如果以上内容无法使用此代码:
drawerListener = new ActionBarDrawerToggle(this, drawerLayout,
getDrawable(R.mipmap.ic_drawer),
getString(R.string.drawer_open),
getString(R.string.drawer_close)){}
答案 3 :(得分:0)
有两个ActionBarDrawerToggle
类。
这是歧义的常见问题。
从构造函数中删除第三个参数可以解决问题。
drawerListener = new ActionBarDrawerToggle(
this,
drawerLayout,
// R.drawable.ic_drawer, <== delete this argument
R.string.drawer_open,
R.string.drawer_close
) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
Toast.makeText(MainActivity.this,"Drawer Open", Toast.LENGTH_LONG).show();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
Toast.makeText(MainActivity.this, "Drawer Closed", Toast.LENGTH_LONG).show();
}
};
经过大量搜索,我有v7。