android - Can't use get arguments to get bundle's data -
when run app crashes though code seems "legit" , there no syntax errors. have built 2 classes:
in first class, preview calling 2 other class made app crashes.
helpfragment.java private void updatetab(string tabid, int placeholder) { fragmentmanager fm = getfragmentmanager(); if (fm.findfragmentbytag(tabid) == null) { bundle bundl = new bundle() ; bundl.putstring("tabid", tabid); mylistfragment list_fragment = new mylistfragment(); list_fragment.setarguments(bundl); fm.begintransaction() .replace(placeholder, list_fragment, tabid) .commit(); } }
the class calling, (and problematic one):
mylistfragment.java import java.util.arraylist; import java.util.list; import android.annotation.suppresslint; import android.content.context; import android.os.bundle; import android.app.listfragment; import android.app.loadermanager.loadercallbacks; import android.content.asynctaskloader; import android.content.loader; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.textview; public class mylistfragment extends listfragment implements loadercallbacks<void> { private static final string tag = "fragmenttabs"; private string mtag; private myadapter madapter; private arraylist<string> mitems; private layoutinflater minflater; private int mposition; private int mtotal; private static final string[] = { "lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "fusce", "pharetra", "luctus", "sodales" }; private static final string[] faq = { "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii", "xiii", "xiv", "xv" }; private static final string[] tips = { "hello" , "bitch" , "ass" , "partners" , "screw", "you" ,"all", "peace" , "out"}; private static final int sleep = 1000; private final int wordbarcolor = r.color.word_bar; private final int numberbarcolor = r.color.number_bar; public mylistfragment() { mtag = getarguments().getstring("tabid"); mtotal = helpfragment.tab_about.equals(mtag) ? about.length : faq.length; } // public mylistfragment(string tag) { // mtag = tag; // mtotal = helpfragment.tab_about.equals(mtag) ? about.length // : about.length; // // log.d(tag, "constructor: tag=" + tag); // } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); // important in order save state across screen // configuration changes example setretaininstance(true); minflater = layoutinflater.from(getactivity()); // need instantiate these first time fragment // created; then, method above rest if (madapter == null) { mitems = new arraylist<string>(); madapter = new myadapter(getactivity(), mitems); } getlistview().setadapter(madapter); // initiate loader background work getloadermanager().initloader(0, null, this); } @override public loader<void> oncreateloader(int id, bundle args) { asynctaskloader<void> loader = new asynctaskloader<void>(getactivity()) { @override public void loadinbackground() { try { // simulate time consuming operation going on in // background thread.sleep(sleep); } catch (interruptedexception e) { } return null; } }; // somehow asynctaskloader doesn't want start job without // calling method loader.forceload(); return loader; } @override public void onloadfinished(loader<void> loader, void result) { // add new item , let adapter know in order refresh // views mitems.add(helpfragment.tab_about.equals(mtag) ? about[mposition] : faq[mposition]); madapter.notifydatasetchanged(); // advance in list 1 step mposition++; if (mposition < mtotal - 1) { getloadermanager().restartloader(0, null, this); log.d(tag, "onloadfinished(): loading next..."); } else { log.d(tag, "onloadfinished(): done loading!"); } } @override public void onloaderreset(loader<void> loader) { } private class myadapter extends arrayadapter<string> { public myadapter(context context, list<string> objects) { super(context, r.layout.list_item, r.id.text, objects); } @override public view getview(int position, view convertview, viewgroup parent) { view view = convertview; wrapper wrapper; if (view == null) { view = minflater.inflate(r.layout.list_item, null); wrapper = new wrapper(view); view.settag(wrapper); } else { wrapper = (wrapper) view.gettag(); } wrapper.gettextview().settext(getitem(position)); wrapper.getbar().setbackgroundcolor( mtag == helpfragment.tab_about ? getresources().getcolor( wordbarcolor) : getresources().getcolor( numberbarcolor)); return view; } } // use wrapper (or view holder) object limit calling // findviewbyid() method, parses entire structure of // xml in search id of view private class wrapper { private final view mroot; private textview mtext; private view mbar; public wrapper(view root) { mroot = root; } public textview gettextview() { if (mtext == null) { mtext = (textview) mroot.findviewbyid(r.id.text); } return mtext; } public view getbar() { if (mbar == null) { mbar = mroot.findviewbyid(r.id.bar); } return mbar; } } }
log:
04-06 22:21:25.079: e/androidruntime(5958): fatal exception: main 04-06 22:21:25.079: e/androidruntime(5958): java.lang.nullpointerexception 04-06 22:21:25.079: e/androidruntime(5958): @ com.example.poca2.mylistfragment. <init>(mylistfragment.java:46) 04-06 22:21:25.079: e/androidruntime(5958): @ com.example.poca2.helpfragment.updatetab(helpfragment.java:61) 04-06 22:21:25.079: e/androidruntime(5958): @ com.example.poca2.helpfragment.onactivitycreated(helpfragment.java:51) 04-06 22:21:25.079: e/androidruntime(5958): @ android.app.fragment.performactivitycreated(fragment.java:1707) 04-06 22:21:25.079: e/androidruntime(5958): @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:921) 04-06 22:21:25.079: e/androidruntime(5958): @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1075) 04-06 22:21:25.079: e/androidruntime(5958): @ android.app.backstackrecord.run(backstackrecord.java:682) 04-06 22:21:25.079: e/androidruntime(5958): @ android.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1455) 04-06 22:21:25.079: e/androidruntime(5958): @ android.app.fragmentmanagerimpl$1.run(fragmentmanager.java:441) 04-06 22:21:25.079: e/androidruntime(5958): @ android.os.handler.handlecallback(handler.java:730) 04-06 22:21:25.079: e/androidruntime(5958): @ android.os.handler.dispatchmessage(handler.java:92) 04-06 22:21:25.079: e/androidruntime(5958): @ android.os.looper.loop(looper.java:137) 04-06 22:21:25.079: e/androidruntime(5958): @ android.app.activitythread.main(activitythread.java:5419) 04-06 22:21:25.079: e/androidruntime(5958): @ java.lang.reflect.method.invokenative(native method) 04-06 22:21:25.079: e/androidruntime(5958): @ java.lang.reflect.method.invoke(method.java:525) 04-06 22:21:25.079: e/androidruntime(5958): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1187) 04-06 22:21:25.079: e/androidruntime(5958): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1003) 04-06 22:21:25.079: e/androidruntime(5958): @ dalvik.system.nativestart.main(native method)
move
mtag = getarguments().getstring("tabid"); mtotal = helpfragment.tab_about.equals(mtag) ? about.length : faq.length;
inside onactivitycreated
. when call new mylistfragment
haven't yet called setarguments, still trying access related bundle, causing npe
Comments
Post a Comment