android - Not able to set text of textview from other activity -


i have main activity:

public class mainactivity extends appcompatactivity {      button btnadd;      int a1 = 10;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          btnadd = (button) findviewbyid(r.id.btnadd);          btnadd.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 final secondact sa = new secondact();                  sa.ttl(a1);               }         });     } } 

and have other activity:

public class secondact extends activity {      public textview txt2;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.second);          txt2 = (textview) findviewbyid(r.id.txt2);     }      public void numsum(int no)     {          txt2.settext(string.valueof(no));     } } 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent" >       <button         android:id="@+id/btnadd"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="addd"></button>      <linearlayout         android:layout_below="@+id/btnadd"         android:layout_width="match_parent"         android:layout_height="match_parent">          <include layout="@layout/second"></include>     </linearlayout> </relativelayout> 

when click on button main activity set textview text of second activity gives me error.

error:

fatal exception: main                                                                         process: com.example.sumdemo, pid: 13809                                                                         java.lang.nullpointerexception                                  @ com.example.sumdemo.mainactivity$1.onclick(mainactivity.java:28)                                                                         @ android.view.view.performclick(view.java:4463)                                                                         @ android.view.view$performclick.run(view.java:18770)                                                                         @ android.os.handler.handlecallback(handler.java:808)                                                                         @ android.os.handler.dispatchmessage(handler.java:103)                                                                         @ android.os.looper.loop(looper.java:193)                                                                         @ android.app.activitythread.main(activitythread.java:5333)                                                                         @ java.lang.reflect.method.invokenative(native method)                                                                         @ java.lang.reflect.method.invoke(method.java:515)                                                                         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:824)                                                                         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:640)                                                                         @ dalvik.system.nativestart.main(native method) 

how can set second activity value main activity without passing data through intent ?

 final secondact sa = new secondact();  sa.ttl(a1); 

never use new operator on class extends activity. have use startactivity start secondact, , provide additional info through intent object


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -