我找到了关于如何为我的Android应用程序创建导航抽屉列表的教程 - http://blog.teamtreehouse.com/add-navigation-drawer-android 我认为这很棒并且进展顺利,我现在有一个导航面板,当点击汉堡图标时,它会拉出我的菜单选项。 但是,我无法让菜单项的点击监听器工作。以下是代码中无效的部分:
private ListView mDrawerList;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private String mActivityTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseController databasecontroller = new DatabaseController(this);
Challenge currentChallenge = databasecontroller.getChallenge("challenge01");
TextView text = (TextView) findViewById(R.id.challengeText);
text.setText(currentChallenge.getName());
mDrawerList = (ListView)findViewById(R.id.navList);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void addDrawerItems() {
String[] pagesArray = { "Home", "Inspire Me", "Timeline" };
mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, pagesArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_SHORT).show();
}
});
}
我已在其上运行调试器,并在
行发现了mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener()
我收到错误:
No such instance field: 'mAdapter'
No such instance field: 'mDrawerList'
但是之前必须先定义它们来制作清单吗?
我已经做了一些挖掘,并看到其他人有类似的错误,建议的答案包括重新启动Android工作室和从gradle中删除ProGuard - 我已经完成但都没有成功。有什么想法吗? (我对android很新,不记得太多Java,所以非常感谢傻瓜的答案!)