java - Android app force close when executed -
execution of following code crashed. there no error messages.
it should open web.java
upon button press , creates key-value.
mainactivity.java
import android.app.activity; import android.content.intent; import android.content.sharedpreferences; import android.os.bundle; import android.preference.preferencemanager; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.spinner; public class mainactivity extends activity { private button btngo; final spinner page = (spinner)findviewbyid(r.id.txtpage); string pagechoice; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final sharedpreferences sharedpref =preferencemanager.getdefaultsharedpreferences(this); btngo = (button)findviewbyid(r.id.btngo); btngo.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { pagechoice = page.getselecteditem().tostring(); //hopefully allow "global" url setting. sharedpreferences.editor editor = sharedpref.edit(); editor.putstring("key1", pagechoice); editor.commit(); startactivity(new intent(mainactivity.this, web.class)); } }); } }
this not suppose in class variables:
final spinner page = (spinner)findviewbyid(r.id.txtpage);
this need inside oncreate()
method. after setcontentview()
, method variable.
findviewbyid()
use context lookup view, , need have context (and layout resource need inflated). context in oncreate
execution, not when class loaded.