2011-07-19 124 views
3

一直在調試...幫助?是的,我知道是什麼原因引起的,但IDK的如何解決它......在TextView上引發NullPointerException setText()

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 

回答

4

你錯過了實際行數據,但我猜有一個錯誤在你的佈局文件中(即你沒有)。

super.onCreate(savedInstanceState);致電您需要撥打setContentView以佈局ID。因爲您沒有設置佈局t =(TextView) findViewById(R.id.textView1);返回null,並且當您嘗試並調用setText時,它失敗。

+0

謝謝,問題解決...幾乎尷尬我以前沒有看到這個... –

+0

很高興提供幫助:有時像這樣的問題只需要一個額外的眼睛。 – Femi