Android Eclipse to Android Studio -
i'm moving app minsdk 8 targetsdk 19 built in eclipse minsdk 14 targetsdk 22 built in android studio, since announcement of withdrawal of eclipse support.
rather allowing android studio perform conversion, wanted create new project , manually port code over.
i'm stuck.
i have main activity extends appcompatactivity , uses navigation drawer , toolbar, works fine. fragment loads ok too, until try reference toolbar.
my main activity code is:
public class mainactivity extends appcompatactivity implements blanktestfragment.onfragmentinteractionlistener { private toolbar toolbar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar = (toolbar) findviewbyid(r.id.tool_bar); setsupportactionbar(toolbar); ... ...
my loaded fragment code is:
public class blanktestfragment extends fragment { ... private toolbar toolbar; ... public static blanktestfragment newinstance(string param1, string param2) { blanktestfragment fragment = new blanktestfragment(); bundle args = new bundle(); args.putstring(arg_param1, param1); args.putstring(arg_param2, param2); fragment.setarguments(args); return fragment; } public blanktestfragment() { // required empty public constructor } public final static string tag = blanktestfragment.class.getsimplename(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (getarguments() != null) { mparam1 = getarguments().getstring(arg_param1); mparam2 = getarguments().getstring(arg_param2); } toolbar = ((appcompatactivity)getactivity()).getsupportactionbar(); }
the last line reports:
incompatible types: required: android.support.v7.widget.toolbar found: android.support.v7.app.actionbar
so, what's going wrong? actionbar reference coming from?
solution
in mainactivity added:
public static toolbar gettoolbar(){ return toolbar; }
in test fragment, in onattach, added:
toolbar = mainactivity.gettoolbar(); toolbar.settitle("hello");
solution 2
it turns out small error in test fragment prevented original implementation working.
in mainactivity had set toolbar supportactionbar. when tried
toolbar = ((appcompatactivity)getactivity()).getsupportactionbar();
in fragment, had set toolbar be
private toolbar toolbar;
but wasn't toolbar, changed to
private actionbar toolbar;
and worked fine.
where actionbar reference coming from?
the return value of getsupportactionbar()
instance of android.support.v7.app.actionbar
, indicated in the javadocs. cannot assign toolbar
field, toolbar
not inherit actionbar
.
under covers, setsupportactionbar()
takes toolbar
, wraps in object. not see easy way toolbar
back. so, try work actionbar
wrapper getsupportactionbar()
returns.
Comments
Post a Comment