android - How to use java classes within one activity? -
how can use java class within 1 activity, mean having different components of activity spread out in bunch of java classes. i'm little new android , have tried far:
mainactivity.java
package com.example.alex.myapplication; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); new something(this); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
something.java
import android.view.view; import android.widget.edittext; import android.widget.button; import android.app.activity; public class { private activity activity; private button add,subtract,multiply,devide; private edittext edita, editb, editc; private double doublea,doubleb,doublec; public something(activity a){ activity=a; click(); } public void click(){ edita = (edittext) activity.findviewbyid(r.id.edittext); editb = (edittext) activity.findviewbyid(r.id.edittext2); editc = (edittext) activity.findviewbyid(r.id.edittext3); doublea =double.parsedouble(edita.gettext().tostring()); doubleb =double.parsedouble(editb.gettext().tostring()); add = (button) activity.findviewbyid(r.id.add); subtract = (button) activity.findviewbyid(r.id.subtract); multiply = (button) activity.findviewbyid(r.id.multiply); devide = (button) activity.findviewbyid(r.id.devide); add.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { doublec = doublea+doubleb; string s = "" + doublec; editc.settext(s); } }); subtract.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { doublec = doublea-doubleb; string s = "" + doublec; editc.settext(s); } }); multiply.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { doublec = doublea*doubleb; string s = "" + doublec; editc.settext(s); } }); devide.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { doublec = doublea/doubleb; string s = "" + doublec; editc.settext(s); } }); } }
so wasn't sure why listeners weren't working on buttons tried passing activity class has listeners added buttons didn't work in fact application won't start in emulator. wanted have "mainactivity" handle "gui" , have "something" class handle listeners no matter can't seem make them communicate 1 form 1 activity.
logcat
caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.view.view android.view.window.findviewbyid(int)' on null object reference @ android.app.activity.findviewbyid(activity.java:2071) @ com.example.alex.myapplication.something.click(something.java:32) @ com.example.alex.myapplication.mainactivity.oncreate(mainactivity.java:19) @ android.app.activity.performcreate(activity.java:5937) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) @ android.app.activitythread.performlaunchactivity (activitythread.java:2251) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2360) @ android.app.activitythread.access$800(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1278) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run (zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
like before post xd 1 java class, call again after pressing buttons, example, can have invisible buttons, radiobuttons whatever want invisible , click turns visible , useful you, here go:
first variable controls activity going do
string num ="";
then have buttons, use 2 of them , others invisible
button bn1; button bn2; button bn3; button bn4; bn3.setvisibility(view.invisible); bn4.setvisibility(view.invisible);
then button code, depends on how many buttons want
button.setonclicklistener(new button onclicklistener(){ public void onclick(){ //get default variable in case string num intent intent = new intent(mainactivity.this, mainactivity.class); num="cero"; intent.putextra("po", num); codigopeticion=2; startactivityforresult (intent,codigopeticion); finish(); break; } } });
this 1 string num:
bundle extras = getintent().getextras(); if (extras!= null) { num =extras.getstring("po"); }
and @ last not least 1 someting depending string:
if (num.matches("cero")){ //do something, enable more buttons, disable radiobuttons, bn3.setvisibility(view.visible); }else if(num.matches("one")){//this string button //do else in same activity, spected enable radiobuttons, show image, etc bn4.setvisibility(view.visible); }else{ //some textview specific title textview.settext("something's wrong"); }
don't forget bn3 , bn4 listeners!
hope helps you, see ya!
Comments
Post a Comment