Android null exception on TextTextTextText

Android Exception pointer exception specified in given text is forced to run in user interface thread

runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String name=portalData.getFullname();

                        setContentView(R.layout.gradebook);
                            tname.setText(name);

                           }
                });

I made a static member of TextView on Create Im doing it

super.onStart ();

    setContentView(R.layout.gradebook);
      tname=(TextView)findViewById(R.id.fullname); 

can't understand what causes this stacktrace

10-15 15:19:07.090: E/AndroidRuntime(1604): FATAL EXCEPTION: main
10-15 15:19:07.090: E/AndroidRuntime(1604): java.lang.NullPointerException
10-15 15:19:07.090: E/AndroidRuntime(1604):     at com.example.campus.Gradebook$ObservableDemo$6.run(Gradebook.java:518)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at android.os.Handler.handleCallback(Handler.java:730)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at android.os.Looper.loop(Looper.java:137)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at java.lang.reflect.Method.invoke(Method.java:525)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-15 15:19:07.090: E/AndroidRuntime(1604):     at dalvik.system.NativeStart.main(Native Method)

here is the xml

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    tools:context=".vbook" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="70dp"
        android:layout_marginTop="40dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#007FFF" />
</RelativeLayout>
+4
source share
3 answers

Cancel these

  TextView tname=(TextView)findViewById(R.id.fullname); 
  setContentView(R.layout.vbook);

findViewById Searches for the view with the identifier specified in the current bloated.

So, if you get NPE even after changing the above two statements, you should check vbook.xmlyours if you have a text representation with id fullname.

onCreate, ui. , .

vbook.xml

   <TextView
    android:id="@+id/textView3"

, TextView tname=(TextView)findViewById(R.id.textView3)

+12

, TextView null. ,

TextView tname=(TextView)findViewById(R.id.fullname);

TextView, . , :

setContentView(R.layout.vbook);    
TextView tname=(TextView)findViewById(R.id.fullname); 
+3

setContentView () findViewById (id)

, , , ,

+1

All Articles