我正在使用listview来增加导航菜单。我在遇到上述错误时 主要活动启动。我正在共享适配器和主要活动的代码。
@BindView(R.id.drawer_layout) DrawerLayout drawerLayout;
//NavigationView navigationView;
@BindView(R.id.toolbar) Toolbar toolbar;
@BindView(R.id.lv_drawer) ListView listView;
@BindView(R.id.dr_profile) ImageButton profileButton;
@BindView(R.id.dr_schedule) ImageButton scheduleButton;
@BindView(R.id.live) ImageButton liveButton;
@BindView(R.id.dr_message) ImageButton messageButton;
@BindView(R.id.dr_logout) ImageButton logoutButton;
private ActionBar actionBar;
ArrayList<String> nav_items;
int[] icons = {R.drawable.time, R.drawable.appointment_history, R.drawable.live_appointment, R.drawable.medicine_reminder,
R.drawable.video, R.drawable.blog, R.drawable.faq, R.drawable.live_appointment};
DrDrawerListAdapter listAdapter;
ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dr_main);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.app_bar_color)));
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.menu);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_main, new DashboardFragment());
ft.commit();
setDrawer();
init();
initClickListener();
}
private void setDrawer(){
if (toggle == null){
toggle = new ActionBarDrawerToggle(DrMainActivity.this,drawerLayout,R.string.open, R.string.close){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
@Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
}
};
drawerLayout.addDrawerListener(toggle);
}
toggle.syncState();
listAdapter = new DrDrawerListAdapter(DrMainActivity.this, nav_items, icons);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new DrDrawerItemClickListener());
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.live:
setLiveQueue();
break;
case R.id.dr_profile:
setProfile();
break;
case R.id.dr_schedule:
setSchedule();
break;
case R.id.dr_message:
setMessage();
break;
case R.id.dr_logout:
setLogout();
break;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item)){
return true;
}switch (item.getItemId()){
default:
return super.onOptionsItemSelected(item);
}
}
private class DrDrawerItemClickListener implements ListView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
private void selectItem(int position){
switch (position){
case 0:
setDashBoard();
drawerLayout.closeDrawers();
break;
case 1:
setAppointment();
drawerLayout.closeDrawers();
break;
case 2:
setLiveQueue();
drawerLayout.closeDrawers();
break;
case 3:
setMessage();
drawerLayout.closeDrawers();
break;
case 4:
setVideos();
drawerLayout.closeDrawers();
break;
case 5:
setBlog();
drawerLayout.closeDrawers();
break;
}
}
}
private void initClickListener(){
profileButton.setOnClickListener(this);
scheduleButton.setOnClickListener(this);
liveButton.setOnClickListener(this);
messageButton.setOnClickListener(this);
logoutButton.setOnClickListener(this);
}
private void init(){
nav_items = new ArrayList<>();
nav_items.add("My Account");
nav_items.add("Search");
nav_items.add("Live Appointmet");
nav_items.add("My Appointments");
nav_items.add("Upgrade to Premium");
nav_items.add("FAQ");
nav_items.add("Vedios");
nav_items.add("Blog");
//icons = getResources().getIntArray(R.array.dr_drawer_icons);
}
private void setDashBoard(){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_main, new DashboardFragment());
ft.addToBackStack(null);
ft.commit();
}
private void setAppointment(){}
private void setProfile(){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame_main, new DrProfileFragment());
ft.addToBackStack(null);
ft.commit();
}
private void setLiveQueue(){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_main, new DrLiveQueueFragment());
ft.addToBackStack(null);
ft.commit();
}
private void setVideos(){}
private void setMessage(){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_main, new DrMessageFragment());
ft.addToBackStack(null);
ft.commit();
}
private void setLogout(){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame_main, new DrLogoutFragment());
ft.addToBackStack(null);
ft.commit();
}
private void setBlog(){}
private void setFaq(){}
private void setContactUs(){}
private void setSchedule() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_main, new DrScheduleFragment());
ft.addToBackStack(null);
ft.commit();
}
} ***这是我的mainactivity文件 包com.example.azim.qphew.Dr.view.uiactivity;
导入butterknife.BindView; 进口黄油刀。黄油刀;
公共类DrMainActivity扩展了AppCompatActivity实现的View.OnClickListener { 私有静态最终字符串TAG = DrMainActivity.class.getSimpleName();
<item>
<guid isPermaLink="false">1006943</guid>
<title>
<![CDATA[
अब 'उड़ें जब-जब जुल्फें तेरी...' पर इस बुजुर्ग कपल का डांस देखिए
]]>
</title>
<pubDate>2018-06-02 12:04:32</pubDate>
<category>
<![CDATA[ viral_news ]]>
</category>
<enclosure url="http://akamaized.net/video/2018_06/02_jun_18_jodey_ka_dance_1024_996.mp4" type="video/mp4"/>
<media:content url="http://akamaized.net/video/2018_06/02_jun_18_buzurg_ka_dance_1024_996.mp4" type="video/mp4" duration="42">
<media:category>viral_news</media:category>
<media:tags>
<![CDATA[
Old couple dance video, Old couple dance viral, Ude ude jab jab zulfen teri song dance
]]>
</media:tags>
<media:keywords>
<![CDATA[
Old couple dance video, Old couple dance viral, Ude ude jab jab zulfen teri song dance
]]>
</media:keywords>
<media:thumbnail url="http://media2.intoday.in/aajtak/images/stories/062018/video_1024_1527921114_749x421.jpeg"/>
</media:content>
</item>
}
答案 0 :(得分:1)
您应该在调用setDrawer方法之前初始化数组
使用setDrawer更改init()订单行
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dr_main);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.app_bar_color)));
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.menu);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_main, new DashboardFragment());
ft.commit();
init();
setDrawer();
initClickListener();
}
答案 1 :(得分:0)
Call init() before setDrawer method.Before initialising the arraylist you are setting adapter with null
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dr_main);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new
ColorDrawable(getResources().getColor(R.color.app_bar_color)));
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.menu);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_main, new DashboardFragment());
ft.commit();
init();
setDrawer();
initClickListener();