android - Null Pointer Exception while trying to use the settext function -
i have 2 navigation tabs , each navigation tab contains listview. each listview made of several components including textview trying edit.
i'm trying settext text view in class defines each fragment null pointer exception.
the following code excerpts:
view pager
<android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/titletasklist" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.viewpager>
each fragment:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <listview android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:divider="#b5b5b5" android:dividerheight="1dp" android:listselector="@drawable/list_selector" /> </linearlayout>
each listview:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:id="@+id/label" android:layout_height="wrap_content" android:background="@drawable/list_selector" android:orientation="horizontal" android:padding="5dip" > <textview android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/thumbnail" android:layout_toleftof="@+id/thumbnail" android:text="address" android:textcolor="#040404" android:typeface="sans" android:textsize="15dip" android:textstyle="bold"/> ....
this code adapter:
public class tabspageradapter extends fragmentpageradapter { public tabspageradapter(fragmentmanager fm) { super(fm); } @override public listfragment getitem(int index) { switch (index) { case 0: return new arabicfragment(); case 1: return new englishfragment(); } return null; } @override public int getcount() { // item count - equal number of tabs return 2; } }
and in class define tabs:
public class tasklistandoptionsactivity extends fragmentactivity implements actionbar.tablistener { private viewpager viewpager; private tabspageradapter madapter; private static actionbar actionbar; private static context context; // tab titles private string[] tabs = { "arabic", "english"}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_task_list_and_options); viewpager = (viewpager) findviewbyid(r.id.titletasklist); actionbar = getactionbar(); getactionbar().setdisplayoptions(actionbar.display_show_custom); getactionbar().setcustomview(r.layout.actionbar); getactionbar().setdisplayshowhomeenabled(true); madapter = new tabspageradapter(getsupportfragmentmanager()); viewpager.setadapter(madapter); actionbar.sethomebuttonenabled(false); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // adding tabs (string tab_name : tabs) { actionbar.addtab(actionbar.newtab().settext(tab_name) .settablistener(this)); } /** * on swiping viewpager make respective tab selected * */ viewpager.setonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpageselected(int position) { // on changing page // make respected tab selected actionbar.setselectednavigationitem(position); } @override public void onpagescrolled(int arg0, float arg1, int arg2) { } @override public void onpagescrollstatechanged(int arg0) { } }); }
....
and in class trying settext (this class each fragment)
public class arabicfragment extends listfragment { public static arraylist<string> tasklistar = new arraylist<string>(); private static context context; private static layoutinflater inflater=null; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.d("changing layout","arabic"); view rootview = inflater.inflate(r.layout.fragment_tasklistar, container, false); return rootview; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); displaytasklist(); arrayadapter<string> adapter = new arrayadapter<string>(getactivity(), r.layout.list_itemar,r.id.title, tasklistar); setlistadapter(adapter); } textview taskdetails = (textview)getview().findviewbyid(r.id.taskdetails); taskdetails.settext("trial");
....
if not right place call settext should called?
you have write in oncreateview method.
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.d("changing layout","arabic"); view rootview = inflater.inflate(r.layout.fragment_tasklistar, container, false); textview taskdetails = (textview)rootview.findviewbyid(r.id.taskdetails); taskdetails.settext("trial"); return rootview; }
Comments
Post a Comment