在TextView setText()上抛出NullPointerException

时间:2011-07-19 02:07:27

标签: android

已经调试了一段时间......帮忙?是的,我知道是什么导致了它,但是如何解决它......

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Random ran = new Random();
    String[] fate = {"Reese","Maak","Buscarino","Gaston","Sakuma","Namjoshi"};
    int i = ran.nextInt(6);
    if(i!=0){
    String stuff =  fate[i];
    TextView t;
    t =(TextView) findViewById(R.id.textView1);
    CharSequence c = stuff;
    t.setText(c);
    }
    if(i==0){
        String stuff =  "Reese";
        TextView t;
        t =(TextView) findViewById(R.id.textView1);
        CharSequence c = stuff;
        t.setText(c);
        }
}

Logcat数据:

07-19 01:58:54.850: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.momentum.wheelofmisfortune/.WomActivity }
    07-19 01:58:55.119: DEBUG/AndroidRuntime(439): Shutting down VM
    07-19 01:58:55.130: DEBUG/dalvikvm(439): Debugger has detached; object registry had 1 entries
    07-19 01:58:55.180: INFO/AndroidRuntime(439): NOTE: attach of thread 'Binder Thread #3' failed
    07-19 01:58:55.490: INFO/ActivityManager(58): Start proc com.momentum.wheelofmisfortune for activity com.momentum.wheelofmisfortune/.WomActivity: pid=446 uid=10035 gids={}
    07-19 01:58:59.160: INFO/ActivityManager(58): Displayed activity com.momentum.wheelofmisfortune/.WomActivity: 3693 ms (total 556891 ms)
    07-19 01:59:05.509: DEBUG/dalvikvm(124): GC_EXPLICIT freed 644 objects / 36600 bytes in 168ms
    07-19 01:59:10.579: DEBUG/dalvikvm(204): GC_EXPLICIT freed 88 objects / 3816 bytes in 160ms
    07-19 01:59:15.499: DEBUG/dalvikvm(265): GC_EXPLICIT freed 64 objects / 3072 bytes in 66ms
    07-19 02:02:37.709: DEBUG/AudioSink(33): bufferCount (4) is too small and increased to 12
    07-19 02:02:37.729: INFO/ActivityManager(58): Starting activity: Intent { cmp=com.momentum.wheelofmisfortune/.generated }
    07-19 02:02:37.879: DEBUG/AndroidRuntime(446): Shutting down VM
    07-19 02:02:37.879: WARN/dalvikvm(446): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446): FATAL EXCEPTION: main
    07-19 02:02:37.899: ERROR/AndroidRuntime(446): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.momentum.wheelofmisfortune/com.momentum.wheelofmisfortune.generated}: java.lang.NullPointerException
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at android.os.Looper.loop(Looper.java:123)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at java.lang.reflect.Method.invoke(Method.java:521)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at dalvik.system.NativeStart.main(Native Method)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446): Caused by: java.lang.NullPointerException
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at com.momentum.wheelofmisfortune.generated.onCreate(generated.java:22)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    07-19 02:02:37.899: ERROR/AndroidRuntime(446):     ... 11 more
    07-19 02:02:37.929: WARN/ActivityManager(58):   Force finishing activity com.momentum.wheelofmisfortune/.generated
    07-19 02:02:37.949: WARN/ActivityManager(58):   Force finishing activity com.momentum.wheelofmisfortune/.WomActivity
    07-19 02:02:38.139: WARN/AudioFlinger(33): write blocked for 84 msecs, 10 delayed writes, thread 0xb3f0
    07-19 02:02:38.449: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{43f6fca0 com.momentum.wheelofmisfortune/.generated}
    07-19 02:02:38.619: ERROR/MP3Extractor(33): Unable to resync. Signalling end of stream.
    07-19 02:02:39.939: INFO/Process(446): Sending signal. PID: 446 SIG: 9
    07-19 02:02:39.990: INFO/ActivityManager(58): Process com.momentum.wheelofmisfortune (pid 446) has died.
    07-19 02:02:39.999: INFO/WindowManager(58): WIN DEATH: Window{43f96690 com.momentum.wheelofmisfortune/com.momentum.wheelofmisfortune.WomActivity paused=true}
    07-19 02:02:40.029: WARN/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 446 uid 10035
    07-19 02:02:48.926: WARN/ActivityManager(58): Activity destroy timeout for HistoryRecord{43e56010 com.momentum.wheelofmisfortune/.WomActivity}
    07-19 02:02:48.930: WARN/ActivityManager(58): Activity destroy timeout for HistoryRecord{43f6fca0 com.momentum.wheelofmisfortune/.generated}
    07-19 02:03:35.550: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol
    07-19 02:03:45.629: DEBUG/dalvikvm(411): GC_EXPLICIT freed 2426 objects / 162784 bytes in 159ms

1 个答案:

答案 0 :(得分:4)

您缺少实际的行数据,但我猜您的布局文件中存在错误(即您没有错误)。

super.onCreate(savedInstanceState);通话后的某个地方,您需要使用布局ID调用setContentView。因为您没有设置布局t =(TextView) findViewById(R.id.textView1);正在返回null,当您尝试调用setText时,它会失败。