I have TextView
within a Fragment
(a flip).
How do I change the text of this TextView
?
Here you are giving NullPointer
qtAllImagensLabel = (TextView)findViewById(R.id.qtAllImagensLabel);
qtAllImagensLabel.setText("texto");
I have already tried to initialize the TextView
within onCreateView()
, and already tried to change the text inside a Handler
.
When I try to change within a Handler
, it does not change but not NullPointer
Edit:
I start the variables globally and in onCreateView()
do they get findViewById()
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_umclique_tab, container, false);
qtAllImagensLabel = (TextView)findViewById(R.id.qtAllImagensLabel);
qtAllVideosLabel = (TextView)findViewById(R.id.qtAllVideosLabel);
qtAllAudioLabel = (TextView)findViewById(R.id.qtAllAudioLabel);
return view;
}
So far so good, but I tried to set the text in and out of onCreate()
, within Button
, but not.
Error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.tupinikimtecnologia.whatsappmediahidder/br.com.tupinikimtecnologia.whatsappmediahidder.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2216)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5140)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at br.com.tupinikimtecnologia.whatsappmediahidder.MainActivity.onCreate(MainActivity.java:103)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2170)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5140)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
If I put it inside a handler, it does not make a mistake, but it does not change the text either.
Fragment Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relative1"
android:layout_marginBottom="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/umclique_tab_descricao_label"
android:id="@+id/textView" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relative2"
android:layout_weight="1"
android:background="@drawable/umclique_tab_border">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/umclique_tab_titulo_imagem"
android:id="@+id/imagensTituloLabel"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/umclique_tab_qt_imagem"
android:id="@+id/visEsconderLabel1"
android:layout_below="@+id/imagensTituloLabel"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/umclique_tab_qt_all_label"
android:id="@+id/qtAllImagensLabel"
android:layout_alignTop="@+id/visEsconderLabel1"
android:layout_toRightOf="@+id/visEsconderLabel1"
android:layout_marginLeft="5dp" />
<Button
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="@string/umclique_tab_botao_revelar"
android:id="@+id/mostrarButton1"
android:layout_below="@+id/visEsconderLabel1"
style="@style/ButtonVerdeMostrar"
android:layout_toLeftOf="@+id/imagensTituloLabel"
android:onClick="mostrarImagensUmCliqueTab" />
<Button
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="@string/umclique_tab_botao_esconder"
android:id="@+id/esconderButton1"
android:layout_below="@+id/visEsconderLabel1"
android:layout_toRightOf="@+id/imagensTituloLabel"
style="@style/ButtonVermelhoEsconder"
android:onClick="esconderImagensUmCliqueTab" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativeLayout"
android:layout_weight="1"
android:background="@drawable/umclique_tab_border">
<Button
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="@string/umclique_tab_botao_revelar"
android:id="@+id/mostrarButton2"
android:layout_alignWithParentIfMissing="false"
android:layout_below="@+id/visEsconderLabel2"
android:layout_toLeftOf="@+id/textView6"
style="@style/ButtonVerdeMostrar"
android:onClick="mostrarVideosUmCliqueTab" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/umclique_tab_titulo_video"
android:id="@+id/textView6"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/umclique_tab_qt_video"
android:id="@+id/visEsconderLabel2"
android:layout_below="@+id/textView6"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/umclique_tab_qt_all_label"
android:id="@+id/qtAllVideosLabel"
android:layout_alignTop="@+id/visEsconderLabel2"
android:layout_toRightOf="@+id/visEsconderLabel2"
android:layout_marginLeft="5dp" />
<Button
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="@string/umclique_tab_botao_esconder"
android:id="@+id/esconderButton2"
android:layout_alignTop="@+id/mostrarButton2"
android:layout_toRightOf="@+id/textView6"
style="@style/ButtonVermelhoEsconder"
android:onClick="esconderVideosUmCliqueTab" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativeLayout2"
android:layout_weight="1"
android:background="@color/preto">
<Button
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="@string/umclique_tab_botao_revelar"
android:id="@+id/mostrarButton3"
android:layout_below="@+id/visEsconderLabel3"
android:layout_toLeftOf="@+id/textView9"
style="@style/ButtonVerdeMostrar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/umclique_tab_titulo_audio"
android:id="@+id/textView9"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/umclique_tab_qt_audio"
android:id="@+id/visEsconderLabel3"
android:layout_below="@+id/textView9"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/umclique_tab_qt_all_label"
android:id="@+id/qtAllAudioLabel"
android:layout_alignTop="@+id/visEsconderLabel3"
android:layout_toRightOf="@+id/visEsconderLabel3"
android:layout_marginLeft="5dp" />
<Button
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="@string/umclique_tab_botao_esconder"
android:id="@+id/esconderButton3"
android:layout_below="@+id/visEsconderLabel3"
android:layout_toRightOf="@+id/textView9"
style="@style/ButtonVermelhoEsconder" />
</RelativeLayout>
</LinearLayout>