强制在 UI 线程中运行的 settext 上引发的 Android Null 指针异常
runOnUiThread(new Runnable() {
@Override
public void run() {
String name=portalData.getFullname();
setContentView(R.layout.gradebook);
tname.setText(name);
}
});
这
我在 Create Im 上做了一个 TextView 的静态成员
super.onStart();
setContentView(R.layout.gradebook);
tname=(TextView)findViewById(R.id.fullname);
无法理解导致此堆栈跟踪的原因
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)
这是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>