android - How to setContentView before super.onCreate while using AppCompat v22.1.0? -
hey i've upgraded app appcompat v22.1.0 , got exception
caused by: java.lang.illegalargumentexception: appcompat not support current theme features @ android.support.v7.app.appcompatdelegateimplv7.ensuresubdecor(appcompatdelegateimplv7.java:360) @ android.support.v7.app.appcompatdelegateimplv7.setcontentview(appcompatdelegateimplv7.java:246) @ android.support.v7.app.appcompatactivity.setcontentview(appcompatactivity.java:106)
i found soln. here https://stackoverflow.com/a/29790071/2781359
still problem wasn't solved because calling setcontentview after super.oncreate, in connectionwifieditactivity
class.
when changed throws nullpointerexception
how can solve this?
caused by: java.lang.nullpointerexception @ client.activity.connection.connectioneditactivity.onresume(connectioneditactivity.java:46) @ client.activity.connection.connectionwifieditactivity.onresume(connectionwifieditactivity.java:81)
connectionwifieditactivity
public class connectionwifieditactivity extends connectioneditactivity implements onclicklistener { private connectionwifi connection; private edittext host; private edittext port; button scan; listview lv; private toolbar mtoolbar; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.setcontentview(r.layout.connectionwifiedit); lv = (listview) findviewbyid(android.r.id.list); this.connection = (connectionwifi) connectionparam; mtoolbar = (toolbar) findviewbyid(r.id.toolbar_actionbar); setsupportactionbar(mtoolbar); getsupportactionbar().setdisplayhomeasupenabled(true); this.host = (edittext) this.findviewbyid(r.id.host); this.port = (edittext) this.findviewbyid(r.id.port); snackbarmanager.show( snackbar.with(getapplicationcontext()) // context .type(snackbartype.multi_line) // set multi-line snackbar .text(r.string.tip) // text displayed .duration(snackbar.snackbarduration.length_indefinite) , this); } public void save(view v){ this.finish(); } @override public void onclick(view v) { } protected void onresume() { super.onresume(); this.host.settext(this.connection.gethost()); this.port.settext(integer.tostring(this.connection.getport())); } protected void onpause() { super.onpause(); this.connection.sethost(this.host.gettext().tostring()); this.connection.setport(integer.parseint(this.port.gettext().tostring())); }}
connectioneditactivity
public static connection connectionparam; private connection connection; private edittext name; private edittext password; public class connectioneditactivity extends appcompatactivity implements onclicklistener { public static connection connectionparam; private connection connection; private button save; private edittext name; private edittext password; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.connection = connectionparam; this.name = (edittext) this.findviewbyid(r.id.name); this.password = (edittext) this.findviewbyid(r.id.password); } protected void onresume() { super.onresume(); this.name.settext(this.connection.getname()); this.password.settext(this.connection.getpassword()); } protected void onpause() { super.onpause(); this.connection.setname(this.name.gettext().tostring()); this.connection.setpassword(this.password.gettext().tostring()); } public void onclick(view v) { if (v == this.save) { this.finish(); } } }
connection
public abstract class connection implements comparable<connection>, serializable { private static final long serialversionuid = 1l; public static final int type_count = 2; public static final int wifi = 0; public static final int bluetooth = 1; private string name; private string password; public connection() { this.name = ""; this.password = remoteitconnection.default_password; } public static connection load(sharedpreferences preferences, connectionlist list, int position) { connection connection = null; int type = preferences.getint("connection_" + position + "_type", -1); switch (type) { case wifi: connection = connectionwifi.load(preferences, position); break; case bluetooth: connection = connectionbluetooth.load(preferences, position); break; } connection.name = preferences.getstring("connection_" + position + "_name", null); connection.password = preferences.getstring("connection_" + position + "_password", null); return connection; } public void save(editor editor, int position) { editor.putstring("connection_" + position + "_name", this.name); editor.putstring("connection_" + position + "_password", this.password); } public abstract remoteitconnection connect(remoteit application) throws ioexception; public abstract void edit(context context); protected void edit(context context, intent intent) { connectioneditactivity.connectionparam = this; context.startactivity(intent); } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public int compareto(connection c) { return this.name.compareto(c.name); } }
since want ui elements in super method, have find way define layout in superclass. reason because getting npe described in other answers.
you can use setcontentview()
in superclass, using method return layout use.
in way can override layout in sub class, overriding method.
for example can use setcontentview(getlayoutid()):
public class connectioneditactivity extends appcompatactivity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(getlayoutid()); //pay attention here... this.connection = connectionparam; this.name = (edittext) this.findviewbyid(r.id.name); this.password = (edittext) this.findviewbyid(r.id.password); } protected int getlayoutid(){ //.... } }
and can override in other activity, can avoid setcontentview
method.
public class connectionwifieditactivity extends connectioneditactivity{ @override protected int getlayoutid(){ return r.layout.connectionwifiedit; } protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //setcontentview(); //comment line //.. } }
Comments
Post a Comment