android - Null Pointer Exception on drawer Layout -
i getting null pointer exception
@ drawerlayout=(drawerlayout) findviewbyid(r.layout.drawer_layout);
please me out. why returning null pointer exception? don't know doing wrong.
package com.example.prototype; import android.app.actionbar; import android.app.activity; import android.os.bundle; import android.support.v4.app.actionbardrawertoggle; import android.support.v4.widget.drawerlayout; import android.support.v4.widget.drawerlayout.drawerlistener; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.arrayadapter; import android.widget.listview; import android.widget.toast; public class mainactivity extends activity { drawerlayout drawerlayout; actionbar action_bar; listview navigation_list; actionbardrawertoggle drawertoggle; string items[]; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.drawer_layout); initcomponents(); action_bar=getactionbar(); setnavigationlist(); } @suppresswarnings("deprecation") private void setnavigationlist() { int open=r.string.navigation_open; int close=r.string.navigation_close; navigation_list.setadapter(new arrayadapter<string>(this, android.r.layout.simple_list_item_1, items)); drawertoggle=new actionbardrawertoggle(this,drawerlayout, r.drawable.ic_menu_white_18dp,open,close){ public void ondrawerclosed(view view) { super.ondrawerclosed(view); action_bar.show(); } public void ondraweropened(view drawerview) { super.ondraweropened(drawerview); action_bar.hide(); } }; if(drawerlayout==null){ toast.maketext(getapplicationcontext(), "null", toast.length_long).show(); } else { drawerlayout.setdrawerlistener(drawertoggle); action_bar.setdisplayhomeasupenabled(true); action_bar.sethomebuttonenabled(true); } } private void initcomponents() { // todo auto-generated method stub drawerlayout=(drawerlayout) findviewbyid(r.layout.drawer_layout); action_bar=getactionbar(); navigation_list=(listview) findviewbyid(r.id.left_drawer); items=getresources().getstringarray(r.array.navigation_drawer); if(drawerlayout==null){ toast.maketext(getapplicationcontext(), "null0", toast.length_long).show(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } if (id == r.id.search_menu) { toast.maketext(getapplicationcontext(), "searched clicked ", toast.length_long).show(); return true; } if (id == r.id.share_menu) { toast.maketext(getapplicationcontext(), "share clicked ", toast.length_long).show(); return true; } if (id == r.id.navigation_menu) { toast.maketext(getapplicationcontext(), "navigation clicked ", toast.length_long).show(); return true; } return super.onoptionsitemselected(item); } }
here xml file
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <framelayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <listview android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choicemode="singlechoice" android:divider="@android:color/transparent" android:dividerheight="5dp" android:background="#e1e1e1"/> </android.support.v4.widget.drawerlayout>
it has
drawerlayout=(drawerlayout) findviewbyid(r.id.drawer_layout);
so r.id instad of r.layout because referring element, not xml file.
Comments
Post a Comment