Sunday, 15 August 2010

java - findViewById returning null (TextView) -



java - findViewById returning null (TextView) -

this question has reply here:

findviewbyid in fragment 21 answers nullpointerexception accessing views in oncreate() 8 answers

i have searched on google stackoverflow. none of solutions fixed problem. findviewbyid returning null when there in oncreate . but, when alter onclick function, works fine. please help me. beginner android programming.

my mainfunction.java

public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } final textview t = (textview) findviewbyid(r.id.textview1); t.settext("hi world."); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { homecoming true; } homecoming super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); homecoming rootview; } }

}

my fragment_main.xml

<linearlayout 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" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.test.mainactivity$placeholderfragment" > <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" android:onclick="onclick" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="108dp" android:layout_margintop="68dp" android:text="textview" />

@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); textview t = (textview) rootview.findviewbyid(r.id.textview1); t.settext("hi world."); homecoming rootview; }

java android textview

No comments:

Post a Comment