java - Set On Click Listener for custom view Crashes App -


this question has answer here:

i attempting create custom container extending linear layout containing few buttons, edit texts, etc., can added app programmatically. far can add custom layout programmatically using layoutinflater, when tried set on click listener button contained inside layout, app crashes every time on startup

here simplified code:

exercise_entry_layout.xml, layout view:

<com.mdmil.app.exerciseentry xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="60dp"     android:orientation="horizontal">      <textview         android:id="@+id/textview"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_weight="1"         android:gravity="center"         android:text="textview" />      <button         android:id="@+id/button"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_weight="1"         android:text="button" />  </com.mdmil.app.exerciseentry> 

exerciseentry.java, java class

public class exerciseentry extends linearlayout {     public exerciseentry(context context) {         this(context, null, 0);     }      public exerciseentry(context context, @nullable attributeset attrs) {         this(context, attrs, 0);     }      public exerciseentry(final context context, @nullable attributeset attrs, int defstyleattr)     {         super(context, attrs, defstyleattr);          button button = findviewbyid(r.id.button);         button.setonclicklistener(new onclicklistener() {             @override             public void onclick(view view) {                 // stuff             }         });     } } 

this how add view app programmatically in mainactivity.java:

view entry = layoutinflater.from(this).inflate(r.layout.exercise_entry_layout, mconstraintlayout, false); mconstraintlayout.addview(entry); 

the logcat output such:

08-14 14:29:14.422 20918-20918/? i/zygote: not late-enabling -xcheck:jni (already on) 08-14 14:29:14.451 20918-20918/? w/zygote: unexpected cpu variant x86 using defaults: x86 08-14 14:29:14.755 20918-20918/com.mdmil.app i/instantrun: starting instant run server: main process 08-14 14:29:15.028 20918-20918/com.mdmil.app d/androidruntime: shutting down vm 08-14 14:29:15.035 20918-20918/com.mdmil.app e/androidruntime: fatal exception: main                                                                process: com.mdmil.app, pid: 20918                                                                java.lang.runtimeexception: unable start activity componentinfo{com.mdmil.app/com.mdmil.app.mainactivity}: android.view.inflateexception: binary xml file line #2: binary xml file line #2: error inflating class com.mdmil.app.exerciseentry                                                                    @ android.app.activitythread.performlaunchactivity(activitythread.java:2817)                                                                    @ android.app.activitythread.handlelaunchactivity(activitythread.java:2892)                                                                    @ android.app.activitythread.-wrap11(unknown source:0)                                                                    @ android.app.activitythread$h.handlemessage(activitythread.java:1593)                                                                    @ android.os.handler.dispatchmessage(handler.java:105)                                                                    @ android.os.looper.loop(looper.java:164)                                                                    @ android.app.activitythread.main(activitythread.java:6540)                                                                    @ java.lang.reflect.method.invoke(native method)                                                                    @ com.android.internal.os.zygote$methodandargscaller.run(zygote.java:240)                                                                    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:767)                                                                 caused by: android.view.inflateexception: binary xml file line #2: binary xml file line #2: error inflating class com.mdmil.app.exerciseentry                                                                 caused by: android.view.inflateexception: binary xml file line #2: error inflating class com.mdmil.app.exerciseentry                                                                 caused by: java.lang.reflect.invocationtargetexception                                                                    @ java.lang.reflect.constructor.newinstance0(native method)                                                                    @ java.lang.reflect.constructor.newinstance(constructor.java:334)                                                                    @ android.view.layoutinflater.createview(layoutinflater.java:650)                                                                    @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:793)                                                                    @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:733)                                                                    @ android.view.layoutinflater.inflate(layoutinflater.java:495)                                                                    @ android.view.layoutinflater.inflate(layoutinflater.java:426)                                                                    @ com.mdmil.app.mainactivity.oncreate(mainactivity.java:29)                                                                    @ android.app.activity.performcreate(activity.java:6980)                                                                    @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1213)                                                                    @ android.app.activitythread.performlaunchactivity(activitythread.java:2770)                                                                    @ android.app.activitythread.handlelaunchactivity(activitythread.java:2892)                                                                    @ android.app.activitythread.-wrap11(unknown source:0)                                                                    @ android.app.activitythread$h.handlemessage(activitythread.java:1593)                                                                    @ android.os.handler.dispatchmessage(handler.java:105)                                                                    @ android.os.looper.loop(looper.java:164)                                                                    @ android.app.activitythread.main(activitythread.java:6540)                                                                    @ java.lang.reflect.method.invoke(native method)                                                                    @ com.android.internal.os.zygote$methodandargscaller.run(zygote.java:240)                                                                    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:767)                                                                 caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.button.setonclicklistener(android.view.view$onclicklistener)' on null object reference                                                                    @ com.mdmil.app.exerciseentry.<init>(exerciseentry.java:56)                                                                    @ com.mdmil.app.exerciseentry.<init>(exerciseentry.java:0)                                                                    @ java.lang.reflect.constructor.newinstance0(native method)                                                                     @ java.lang.reflect.constructor.newinstance(constructor.java:334)                                                                     @ android.view.layoutinflater.createview(layoutinflater.java:650)                                                                     @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:793)                                                                     @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:733)                                                                     @ android.view.layoutinflater.inflate(layoutinflater.java:495)                                                                     @ android.view.layoutinflater.inflate(layoutinflater.java:426)                                                                     @ com.mdmil.app.mainactivity.oncreate(mainactivity.java:29)                                                                     @ android.app.activity.performcreate(activity.java:6980)                                                                     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1213)                                                                     @ android.app.activitythread.performlaunchactivity(activitythread.java:2770)                                                                     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2892)                                                                     @ android.app.activitythread.-wrap11(unknown source:0)                                                                     @ android.app.activitythread$h.handlemessage(activitythread.java:1593)                                                                     @ android.os.handler.dispatchmessage(handler.java:105)                                                                     @ android.os.looper.loop(looper.java:164)                                                                     @ android.app.activitythread.main(activitythread.java:6540)                                                                     @ java.lang.reflect.method.invoke(native method)                                                                     @ com.android.internal.os.zygote$methodandargscaller.run(zygote.java:240)                                                                     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:767)  08-14 14:29:15.061 20918-20925/com.mdmil.app w/zygote: suspending threads took: 17.310ms 

i moderately new android, feedback on implementation welcome. thank you!

hard without logs sure, calling constructor of exerciseentry trying setup listener button not created yet

make sure inflate layout before setup listener


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -