java - Fragment View Return Null -
my view has been returning null though have set view global variable , calling findviewbyid on oncreateview().
this codes view part
public class mainmenu extends fragment { textview txt = null; view view1; view mview; public view oncreateview(layoutinflater inflater,viewgroup container, bundle savedinstancestate) { // view1 = inflater.inflate(r.layout.fragment_main_menu, container, false); mview = layoutinflater.from(getactivity()).inflate(r.layout.fragment_main_menu, null,false); pref = getactivity().getpreferences(0); twitter = new twitterfactory().getinstance(); twitter.setoauthconsumer(pref.getstring("consumer_key", ""), pref.getstring("consumer_secret", "")); txt = (textview)mview.findviewbyid(r.id.tww1); //run retrieve tweets method new tweetstream().execute(); new thread(new mainmenu().new consumer()).start(); return mview; }
in fragment_main_menu.xml
<linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tww1" android:gravity="center" android:textsize="100dp" android:text="off"/> </linearlayout>
and codes invoke it:
public void turnon(){ txt.settext("on"); }
the method runs turnon function:
class consumer implements runnable { @override public void run() { try { while (queue.size() != 0) { string msg = getobj.getmsg(); string[] result = msg.split("\\s"); string msg2 = result[0]; if(msg2.equals("on")){ txt.settext("on"); //turnon(); } else if(msg.equals("off")){ txt.settext("off"); // turnoff(); } } if (queue.size() == 0) { thread.sleep(1000); run(); } } catch (interruptedexception e) { e.printstacktrace(); } } }
and error get:
06-19 12:47:43.921 5213-9586/? e/androidruntime﹕ fatal exception: thread-128222 process: com.example.john.fabric, pid: 5213 java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settext(java.lang.charsequence)' on null object reference
any ideas? thank you!
edit1: using false inflater inflate method. not see why should null pointer.
edit2: running turnon or settext method inside both return me null.
use runonui thread update values ui part try below code
@override public void run() { try { while (queue.size() != 0) { string msg = getobj.getmsg(); string[] result = msg.split("\\s"); string msg2 = result[0]; runonuithread(new runnable() { @override public void run() { if(msg2.equals("on")){ txt.settext("on"); //turnon(); } else if(msg.equals("off")){ txt.settext("off"); // turnoff(); } } } } if (queue.size() == 0) { thread.sleep(1000); run(); } } catch (interruptedexception e) { e.printstacktrace(); }
Comments
Post a Comment