java - Get textView value from fragment to MainActivity -


i using fragments. have textview in fragment , want value in main activity.

this fragment layout

<gridlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/gridlayout1"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_marginbottom="30dp"     android:layout_marginend="50dp"     android:layout_marginstart="55dp"     android:layout_margintop="10dp"     android:columncount="20"     android:orientation="horizontal"     android:rowcount="16"     tools:context="com.lipi.worldofelements.mainfragment">      <space         android:layout_width="25dp"         android:layout_height="35dp"         android:layout_column="2"         android:layout_row="2"         android:background="@color/colorbuttonnobelgasses" />      <linearlayout         android:layout_width="505dp"         android:layout_height="165dp"         android:layout_column="4"         android:layout_columnspan="13"         android:layout_margin="5dp"         android:layout_row="2"         android:layout_rowspan="5"         android:orientation="horizontal"          >          <relativelayout             android:id="@+id/btnelement"             android:layout_width="165dp"             android:layout_height="match_parent"             android:background="@color/colorbuttonnonmetals"             android:clickable="true"               >              <textview                 android:id="@+id/txtelemsym"                 style="@style/apptheme"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:layout_marginbottom="25dp"                 android:clickable="true"                 android:gravity="center"                 android:linksclickable="true"                 android:onclick="btnclikcedtoast"                 android:text="@string/h"                 android:textcolor="#ffff"                 android:textsize="@dimen/buttonxl"                  /> 

i loading fragment function inside mainactivity.java:

    //set fragment activity          mainfragment fragment = new mainfragment();         fragmenttransaction fragmenttransaction =                 getsupportfragmentmanager().begintransaction();         fragmenttransaction.replace(r.id.fragment_container, fragment);         fragmenttransaction.commit(); 

this fragment class

  public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         view rootview = inflater.inflate(r.layout.fragment_main, container, false);         databasehelper = new elementdatabaseadapter(getactivity());       txtelemsym = (textview) rootview.findviewbyid(r.id.txtelemsym);        return rootview; 

when click textview(txtelemsym) main activity runs "btnclikcedtoast" function inside mainactivity .

  public void btnclikcedtoast(view view) {          txtsymbol = (textview) findviewbyid(r.id.txtelemsym);         string elemsymbol = (string) txtsymbol.gettext();          bundle b;         intent = new intent(this, singleelementdata.class);         b = activityoptions.makescaleupanimation(view, 0, 0, view.getwidth(), view.getheight()).tobundle();         i.putextra("symbol", elemsymbol);         startactivity(i, b);      } 

i want textvalue in function. got error "nullpointerexception" after run this.

how can this?


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -