Android: Can't update textview in Fragment from Activity. NullPointerException -
i'm trying update textview in fragment activity. i'm getting nullpointerexception when calling settext method. have tried following, still getting npe. 1. tried accessing fragments textview findviewbyid in activity. 2. tried using method in fragment , calling activity , passing value parameters
fraghome activity
public class fraghome extends appcompatactivity implements tablayout.ontabselectedlistener { handler bluetoothin; private static textview tmpf, humf, cof; string tempgl, humgl, cogl, devname; double dbltemp, dblco; final int handlerstate = 0; // used identify handler message private bluetoothadapter btadapter = null; private tablayout tablayout; private viewpager viewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_frag_home); //adding toolbar activity toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); //initializing tablayout tablayout = (tablayout) findviewbyid(r.id.tablayout); //initializing viewpager viewpager = (viewpager) findviewbyid(r.id.pager); //adding tabs using addtab() method tablayout.addtab(tablayout.newtab().settext("temperature")); tablayout.addtab(tablayout.newtab().settext("humidity")); tablayout.addtab(tablayout.newtab().settext("co")); tablayout.settabgravity(tablayout.gravity_fill); //creating our pager adapter pager adapter = new pager(getsupportfragmentmanager(), tablayout.gettabcount()); //adding adapter pager viewpager.setadapter(adapter); viewpager.setonpagechangelistener(new tablayout.tablayoutonpagechangelistener(tablayout)); //adding ontabselectedlistener swipe views tablayout.setontabselectedlistener(this); bluetoothin=new handler() { string readmessage; string[] values = new string[]{""}; public void handlemessage(android.os.message msg) { if (msg.what == handlerstate) { readmessage = (string) msg.obj; values = readmessage.split("#"); (int j = 0; j < values.length; j++) { int rem = j % 3; if (rem == 0) { tmpf.settext(values[j] + " c"); tempgl = string.valueof(values[j]); try { dbltemp = double.parsedouble(tempgl); } catch (numberformatexception e) { e.printstacktrace(); } } else if (rem == 1) { cof.settext(values[j] + " ppm"); cogl = values[j]; try { dblco = double.parsedouble(cogl); } catch (numberformatexception e) { e.printstacktrace(); } } else if (rem == 2) { humf.settext(values[j] + " %"); humgl = values[j]; } } } } }; btadapter=bluetoothadapter.getdefaultadapter(); // bluetooth } @override public void ontabselected(tablayout.tab tab) { // mviewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(tablayout.tab tab) { } @override public void ontabreselected(tablayout.tab tab) { } }
pager
public class pager extends fragmentstatepageradapter { //integer count number of tabs int tabcount; //constructor class public pager(fragmentmanager fm, int tabcount) { super(fm); //initializing tab count this.tabcount= tabcount; } //overriding method getitem @override public fragment getitem(int position) { //returning current tabs switch (position) { case 0: tab1 tab1 = new tab1(); return tab1; case 1: tab2 tab2 = new tab2(); return tab2; case 2: tab3 tab3 = new tab3(); return tab3; default: return null; } } //overriden method getcount number of tabs @override public int getcount() { return tabcount; } }
tab1
public class onefragment extends fragment { view view; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view = inflater.inflate(r.layout.fragment_one, container, false); return view; } }
fragment_one.xml
<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" tools:context="shinil.tablayout.onefragment" android:id="@+id/rltnvnv"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="temperature" android:textsize="40dp" android:textstyle="bold" android:id="@+id/textviewtemp" android:layout_centerinparent="true"/> </relativelayout>
frag_home.xml
linearlayout android:id="@+id/main_layout" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity"> <!-- our toolbar --> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorprimary" android:minheight="?attr/actionbarsize" android:theme="@style/themeoverlay.appcompat.dark.actionbar" app:popuptheme="@style/themeoverlay.appcompat.light"/> <!-- our tablayout display tabs --> <android.support.design.widget.tablayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorprimary" android:minheight="?attr/actionbarsize" android:theme="@style/themeoverlay.appcompat.dark.actionbar"/> <!-- view pager swipe views --> <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="fill_parent"/> </linearlayout>
the npe occurs when try settext activity. please help
use below callback:
fragment class:
public class fragmentone extends fragment { private viewcallback mcallback; public fragmentone(viewcallback mcallback) { this.mcallback = mcallback; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view = inflater.inflate(r.layout.fragment_one, container, false); mcallback.updatetextview((textview) view.findviewbyid(r.id.fragmenttextview)); return view; } public interface viewcallback { void updatetextview(textview view); } }
below activity class:
public class maincallbackactivity extends activity implements callbackfragment.viewcallback { public textview textview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_callback); fragmentone fragment = new fragmentone(this); getfragmentmanager().begintransaction().add(r.id.framelayout, fragment).commit(); } @override protected void onresume() { super.onresume(); if (textview != null) textview.settext("updating fragment textview in activity..!!"); } @override public void updatetextview(textview view) { this.textview = view; } }
implment call in activity class..then update textview.
Comments
Post a Comment