java - How setText to custom dialog from one class in others? -


i have created dialog class use in several other classes , depending in class should display different info. have no problem displaying dialog working button , set text xml. when try set own text settext app crashes , give me java.lang.nullpointerexception. can problem ?

here custom dialog class

public class customdialoginfoclass extends dialog implements     view.onclicklistener { button ok; textview mytextview; typeface myfont;   public customdialoginfoclass(context context) {     super(context); }      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         requestwindowfeature(window.feature_no_title);         setcontentview(r.layout.custom_dialog_not_demo);         customdialoginfoclass c = new customdialoginfoclass(getcontext());          ok = (button)findviewbyid(r.id.btnok);         mytextview = (textview) c.findviewbyid(r.id.textviewdialog);         ok.setonclicklistener(this);          myfont = typeface.createfromasset(getcontext().getassets(), "fonts/font.ttf");         ok.settypeface(myfont);         mytextview.settypeface(myfont);      }      @override     public void onclick(view v) {         switch (v.getid()){             case r.id.btnok:                 dismiss();                 break;             default:         }         dismiss();     } } 

and here main try call it;

public class mainactivity extends actionbaractivity {  textview mytextview;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      customdialoginfoclass dialoginfo = new customdialoginfoclass(this);     mytextview = (textview) findviewbyid(r.id.textviewdialog);      mytextview.settext("lorem ipsum dolor sit amet"); <----null.pointer error      dialoginfo.show(); } 

xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="wrap_content"     android:layout_height="200dp"     android:orientation="vertical"     android:background="@drawable/diabox"     android:weightsum="1">  <textview     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:textappearance="?android:attr/textappearancelarge"     android:id="@+id/textviewdialog"     android:layout_margin="15dp"     android:textcolor="@android:color/white"     android:text="test text" />  <button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="ok"     android:id="@+id/btnok"     android:layout_gravity="center_horizontal" /> 

and logcat if helps?

java.lang.runtimeexception: unable start activity componentinfo{com.example.name.appname/com.example.name.appname.mainactivity}: java.lang.nullpointerexception             @ android.app.activitythread.performlaunchactivity(activitythread.java:2221)             @ android.app.activitythread.handlelaunchactivity(activitythread.java:2280)             @ android.app.activitythread.access$800(activitythread.java:141)             @ android.app.activitythread$h.handlemessage(activitythread.java:1202)             @ android.os.handler.dispatchmessage(handler.java:102)             @ android.os.looper.loop(looper.java:136)             @ android.app.activitythread.main(activitythread.java:5064)             @ java.lang.reflect.method.invokenative(native method)             @ java.lang.reflect.method.invoke(method.java:515)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:797)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:613)             @ dalvik.system.nativestart.main(native method)      caused by: java.lang.nullpointerexception             @ com.example.name.appnamne.mainactivity.oncreate(mainactivity.java:35)             @ android.app.activity.performcreate(activity.java:6084)             @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087)             @ android.app.activitythread.performlaunchactivity(activitythread.java:2178)             at android.app.activitythread.handlelaunchactivity(activitythread.java:2280)             at android.app.activitythread.access$800(activitythread.java:141)             at android.app.activitythread$h.handlemessage(activitythread.java:1202)             at android.os.handler.dispatchmessage(handler.java:102)             at android.os.looper.loop(looper.java:136)             at android.app.activitythread.main(activitythread.java:5064)             at java.lang.reflect.method.invokenative(native method)             at java.lang.reflect.method.invoke(method.java:515)             at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:797)             at com.android.internal.os.zygoteinit.main(zygoteinit.java:613)             at dalvik.system.nativestart.main(native method) 

you shouldn't access text view activity

in dialog have constructor

public customdialoginfoclass(context context) { super(context); }

so make 1 too:

public customdialoginfoclass(context context,string text) {     customdialoginfoclass(context);     this.text = text; } 

and make string field in dialog class

string text; 

and setup textview in dialog

mytextview = (textview) c.findviewbyid(r.id.textviewdialog); mytextview.settext(text); 

and in activity pass text here:

customdialoginfoclass c = new customdialoginfoclass(getcontext(), "my text"); 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Website Login Issue developed in magento -

Can the constants be defined inside a model file of a framework in PHP? -