android - Null pointer in textview -
i'm new on android , i'm trying first app.
i've read lot of posts here same problem got, couldn't figure out.
i hope can help me.
my app has several activities. in case in particular, i'm sending string want set in textview. when tried this, app crashed. so, read lot of posts , "catched" null pointer. meant setcontentview not working.
i'm working adt downloaded on 2014-06-21. tried clean eclipse, remains same. tried alter name of tvgrupo (before, textview1).
also, i'm not @ english, can tell , more important, since i'm new in android i'm not utilize words used commonly describe process... :( so, please specific.
thanks help!
this code:
public class displaygrup extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_display_grup); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } textview tvgrupo = (textview) findviewbyid(r.id.tvgrupo); if (tvgrupo == null) { // how know received null pointer. toast.maketext(getapplicationcontext(), "puntero nulo", toast.length_short).show(); } else { tvgrupo.settext("hola"); toast.maketext(getapplicationcontext(), "obteniendo dato:" + tvgrupo.tostring(), toast.length_short).show(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.display_grup, 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_display_grup, container, false); homecoming rootview; } } public void clickborrar(view view) { // works fine. textview tv = (textview) findviewbyid(r.id.tvgrupo); tv.settext("hhhh"); } } this 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" android:gravity="top" 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.mula_saltadora.displaygrup$placeholderfragment" > <button android:id="@+id/button1" style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:onclick="clickborrar" android:text="borrar" /> <textview android:id="@+id/tvgrupo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignbottom="@+id/button1" android:layout_alignparenttop="true" android:layout_margintop="10dp" android:layout_toleftof="@+id/button1" android:text="grupo" android:textappearance="?android:attr/textappearancelarge" /> <edittext android:id="@+id/edittext1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:ems="10" > <requestfocus /> </edittext> <button android:id="@+id/button2" style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/edittext1" android:layout_alignparentright="true" android:text="agregar" /> <listview android:id="@+id/listview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignleft="@+id/tvgrupo" android:layout_below="@+id/tvgrupo" android:layout_above="@+id/edittext1" > </listview> </relativelayout>
your textview in fragment layout set code in oncreateview instead in oncreate:
view rootview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { rootview = inflater.inflate(r.layout.fragment_display_grup, container, false); textview tvgrupo = (textview) rootview.findviewbyid(r.id.tvgrupo); if (tvgrupo == null) { // how know received null pointer. toast.maketext(getactivity(), "puntero nulo", toast.length_short).show(); } else { tvgrupo.settext("hola"); toast.maketext(getactivity(), "obteniendo dato:" + tvgrupo.tostring(), toast.length_short).show(); } homecoming rootview; } public void clickborrar(view view) { // works fine. textview tv = (textview) rootview.findviewbyid(r.id.tvgrupo); tv.settext("hhhh"); } edit: alter getapplicationcontext() getactivity() , create rootview global variable in fragment , utilize in function clickborrar too. see edited code.
android nullpointerexception textview
No comments:
Post a Comment