Thursday 15 April 2010

java - android, null pointer of 1 class when trying to open same pop-up from another object -



java - android, null pointer of 1 class when trying to open same pop-up from another object -

sorry because coming stupid questions, first time here.i seek first explain application. have 3 objects ( or 3 java class in android ) every class imageview textview ( first 2 class, lastly class imageview ).i succeed open same pop-up alter values of text views or doing other stuffs. have 3 images views fiew text views want open same pop-up , alter values of textviews or changing backgrounds etc.i have implemented 2 classes , when open pop-up 1 object null other one, same sec object when open null pointer @ first object.i set commented lines of code below:

mainactivity:

relativelayout mylayout; //getting layout mylayout = (relativelayout) rootview.findviewbyid(r.id.mylayout); relativelayout inflator = new relativelayout(getactivity().getapplicationcontext()); relativelayout.layoutparams lp = new relativelayout.layoutparams(layoutparams.wrap_content,layoutparams.wrap_content); lp.addrule(relativelayout.center_in_parent); inflator.setlayoutparams(lp); mylayout.addview(inflator); inflator.setbackgroundcolor(color.cyan); temperaturesymbol mytempsymbol = new temperaturesymbol(getactivity(), inflator); humiditysymbol myhumiditysymbol = new humiditysymbol(getactivity(), inflator);

ok, in mainactivity creating objects , add together them layout, want object stick in tiny layout , move layout, inflator layout adding them.

temperaturesymbol.class

public class temperaturesymbol extends linearlayout{ private int[] location = new int[2]; private imageview temperature; private int value = 20; private gesturedetectorcompat mdetector; private controllistener controllistener; private textview celsius; public interface controllistener { public void onlongstatement(); } public void setcontrollistener(controllistener controllistener) { this.controllistener = controllistener; } public temperaturesymbol getsymbol(){ homecoming this; } void setvalue(int counter){ value = counter; celsius.settext("" + value + "°c"); }//this of import because method set value on textview when increment/decrement button public temperaturesymbol(context context, relativelayout inflator) { super(context); // todo auto-generated constructor stub temperature = (imageview) new imageview(context); temperature.setbackgroundresource(drawable.termometruoff); celsius = new textview(context); inflator.addview(temperature); celsius.settext("°c"); inflator.addview(celsius); inflator.getlocationonscreen(location); //gather informations gesturelistener mygesturelistener gestlist = new mygesturelistener(location, context, inflator); gestlist.settemperaturesymbol(this); mdetector = new gesturedetectorcompat(context, gestlist); temperature.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub mdetector.ontouchevent(event); log.i("test","simple touch"); homecoming true; } }); } }

temperaturesymbol.class 1 of object, imageview textview display value like: 20 degrees celsius.basically humiditysymbol.class same temperature, same textview same imageview image changed.

below mygesturelistener class open same pop-up window both objects.

class mygesturelistener extends gesturedetector.simpleongesturelistener { // handler used onlongpress go on decrementin/incrementing value public class rptupdater implements runnable { public void run() { if (mautoincrement && tvalue < 35) { increment(); repeatupdatehandler.postdelayed(new rptupdater(), rep_delay); } else if (mautodecrement && tvalue > 10) { decrement(); repeatupdatehandler.postdelayed(new rptupdater(), rep_delay); } } private void decrement() { // todo auto-generated method stub if (tvalue > 10) { tvalue--; localtempsymbol.setvalue(tvalue); } } private void increment() { // todo auto-generated method stub if (tvalue < 35) { tvalue++; localtempsymbol.setvalue(tvalue); } } // handler used onlongpress public class rptupdaterhumidity implements runnable { public void run() { if (mautoincrementhumidity && hvalue < 100) { increment(); repeatupdatehandler.postdelayed(new rptupdaterhumidity(), rep_delay); } else if (mautodecrementhumidity && hvalue > 0) { decrement(); repeatupdatehandler.postdelayed(new rptupdaterhumidity(), rep_delay); } } private void decrement() { // todo auto-generated method stub if (hvalue > 0) { hvalue--; localhumsymbol.sethumidity(hvalue); } } private void increment() { // todo auto-generated method stub if (hvalue < 100) { hvalue++; localhumsymbol.sethumidity(hvalue); } } public void settemperaturesymbol(temperaturesymbol tempsymbol) { localtempsymbol = tempsymbol; } public void sethumiditysymbol(humiditysymbol tempsymbol) { localhumsymbol = tempsymbol; } public mygesturelistener(int[] showlocation, context context, relativelayout inflator) { super(); add together = new button(context); sub = new button(context); addh = new button(context); subh = new button(context); popup = new popupwindow(context); popup.setbackgrounddrawable(new shapedrawable()); popup.setoutsidetouchable(true); popup.setbackgrounddrawable(new colordrawable( android.graphics.color.transparent)); layout = new linearlayout(context); layout.addview(add); layout.addview(sub); layout.addview(addh); layout.addview(subh); popup.setcontentview(layout); add.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub if (tvalue < maxtvalue) { tvalue++; localtempsymbol.setvalue(tvalue); } } }); sub.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub if (tvalue > mintvalue) { tvalue--; log.i("test", "localtemp = " + localtempsymbol); localtempsymbol.setvalue(tvalue); } } }); // long click listener enable value increment add.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { mautoincrement = true; repeatupdatehandler.post(new rptupdater()); homecoming false; } }); add.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub if ((event.getaction() == motionevent.action_up || event .getaction() == motionevent.action_cancel) && mautoincrement) mautoincrement = false; homecoming false; } }); // long click listener enable value decrement sub.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { mautodecrement = true; repeatupdatehandler.post(new rptupdater()); homecoming false; } }); sub.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub if ((event.getaction() == motionevent.action_up || event .getaction() == motionevent.action_cancel) && mautodecrement) mautodecrement = false; homecoming false; } }); addh.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub if (hvalue < 100) { hvalue++; localhumsymbol.sethumidity(hvalue); } } }); subh.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub if (hvalue > 0) { hvalue--; log.i("test", "localtemp = " + localhumsymbol); localhumsymbol.sethumidity(hvalue); } } }); // long click listener enable value increment addh.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { mautoincrementhumidity = true; repeatupdatehandlerhumidity.post(new rptupdaterhumidity()); homecoming false; } }); addh.setontouchlistener(new view.ontouchlistener() { @suppresslint("clickableviewaccessibility") @override public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub if ((event.getaction() == motionevent.action_up || event .getaction() == motionevent.action_cancel) && mautoincrementhumidity) mautoincrementhumidity = false; homecoming false; } }); // long click listener enable value decrement subh.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { mautodecrementhumidity = true; repeatupdatehandlerhumidity.post(new rptupdaterhumidity()); homecoming false; } }); subh.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub if ((event.getaction() == motionevent.action_up || event .getaction() == motionevent.action_cancel) && mautodecrementhumidity) mautodecrementhumidity = false; homecoming false; } }); } @override public void onlongpress(motionevent event) { //here read in logcat illustration if open pop-up temperaturesymbol humidity null popup.showatlocation(layout, gravity.top | gravity.left, 10, 10); popup.update(location[0], location[1], layoutparams.wrap_content, layoutparams.wrap_content); } }

i have problem days , couldn't find solution, ideea had alter values through setter method of every symbol.i don't want everywhint in 1 class, want maintain them rid of null pointer.

update:

when running application, open pop-up first symbol , trying modify second:

10-30 09:35:17.583: i/test(12117): localtemp = com.example.symbol_temp_med.temperaturesymbol{4246d188 v.e..... ......i. 0,0-0,0} 10-30 09:35:21.997: i/test(12117): localtemp = null 10-30 09:35:21.997: d/androidruntime(12117): shutting downwards vm 10-30 09:35:21.997: w/dalvikvm(12117): threadid=1: thread exiting uncaught exception (group=0x41b72ba8) 10-30 09:35:22.007: e/androidruntime(12117): fatal exception: main 10-30 09:35:22.007: e/androidruntime(12117): process: com.example.symbol_temp_med, pid: 12117 10-30 09:35:22.007: e/androidruntime(12117): java.lang.nullpointerexception 10-30 09:35:22.007: e/androidruntime(12117): @ com.example.symbol_temp_med.mygesturelistener$9.onclick(mygesturelistener.java:348) 10-30 09:35:22.007: e/androidruntime(12117): @ android.view.view.performclick(view.java:4438) 10-30 09:35:22.007: e/androidruntime(12117): @ android.view.view$performclick.run(view.java:18422) 10-30 09:35:22.007: e/androidruntime(12117): @ android.os.handler.handlecallback(handler.java:733) 10-30 09:35:22.007: e/androidruntime(12117): @ android.os.handler.dispatchmessage(handler.java:95) 10-30 09:35:22.007: e/androidruntime(12117): @ android.os.looper.loop(looper.java:136) 10-30 09:35:22.007: e/androidruntime(12117): @ android.app.activitythread.main(activitythread.java:5001) 10-30 09:35:22.007: e/androidruntime(12117): @ java.lang.reflect.method.invokenative(native method) 10-30 09:35:22.007: e/androidruntime(12117): @ java.lang.reflect.method.invoke(method.java:515) 10-30 09:35:22.007: e/androidruntime(12117): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) 10-30 09:35:22.007: e/androidruntime(12117): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) 10-30 09:35:22.007: e/androidruntime(12117): @ dalvik.system.nativestart.main(native method)

the error line indicate here:

localhumsymbol.sethumidity(hvalue);

java android

No comments:

Post a Comment