java - mapping text to buttons from within a method -
i'm in stages of developing android app, , i'm trying settext buttons within method. doing not in method fine, want call method on , over. when try create method this, "non-static method findviewbyid(int) cannot referenced static context" error.
eg: button button = (button) findviewbyid(r.id.button); button.settext(options[0]);
the findviewbyid red highlighted
package com.example.lp1; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.textview; import java.util.arraylist; import java.util.arrays; import java.util.random; public class mainactivity extends appcompatactivity { private button button; private button button2; private button button3; private button button4; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); int score = 0; int count = 3; static int getquestion(){ arraylist set = addingthree(); final int = (int) set.get(0); final int b = (int) set.get(1); final int c = (int) set.get(2); final int d = (int) set.get(3); final int ans = (int) set.get(4); string qtext = (string) set.get(5); string[] options = new string[] {integer.tostring(a), integer.tostring(b),integer.tostring(c),integer.tostring(d)}; button button = (button) findviewbyid(r.id.button); button.settext(options[0]); button button2 = (button) findviewbyid(r.id.button2); button2.settext(options[1]); button button3 = (button) findviewbyid(r.id.button3); button3.settext(options[2]); button button4 = (button) findviewbyid(r.id.button4); button4.settext(options[3]); textview textview = (textview)findviewbyid(r.id.textview); textview.settext(qtext); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { } }); button2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { } }); button3.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { } }); button4.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { } }); } static arraylist addingthree(){ random rand = new random(); int numa = rand.nextint(99); int numb = rand.nextint(99); int numc = rand.nextint(99); int ans = numa + numb + numc; int fake1 = rand.nextint(200) + 50; int fake2 = rand.nextint(200) +50; int fake3 = rand.nextint(200) +50; int[ ] options = {fake1, fake2, fake3, ans}; string stringquestion = ("what sum of " + numa + ", " + numb + ", , " + numc + "? "); arrays.sort(options); arraylist parcel = new arraylist(); parcel.add(options[0]); parcel.add(options[1]); parcel.add(options[2]); parcel.add(options[3]); parcel.add(ans); parcel.add(stringquestion); return parcel; }
}
remove
static
from functions. remove functions oncreate callback, don't belong there.
when you're invoking functions in oncreate, functions/methods cannot static
Comments
Post a Comment