2012-05-22 72 views
0

我試圖創建一個HelloWorld應用程序,該按鈕用於單擊按鈕時用當前時間更新文本字段。它似乎很好(日食發現沒有錯誤),但每次我運行它,我的手機部隊崩潰....爲什麼?!?!?!Eclipse沒有看到錯誤... Android部隊關閉

反正...

這裏是java文件

package com.HelloWorld.test; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import java.util.Date; 

public class HelloWorldActivity extends Activity implements View.OnClickListener{ 

Button btn; 
EditText ET; 

@Override 
public void onCreate(Bundle icicle){ 
    super.onCreate(icicle); 

    btn = (Button) findViewById(R.id.btn); 
    btn.setOnClickListener(this); 
    ET = (EditText) findViewById(R.id.ET); 
    updateTime(); 
    setContentView(R.layout.main); 

} 

public void onClick(View view){ 
    updateTime(); 
} 

public void updateTime(){ 
    ET.setText(new Date().toString()); 
} 

} 

這裏是.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 


<Button 
    android:id= "@+id/btn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="100dp" 
    android:text="@string/UpdateTime" /> 

<EditText 
    android:id="@+id/ET" 
    android:inputType = "text" 
    android:layout_width = "fill_parent" 
    android:layout_height="wrap_content"/> 

</LinearLayout> 

logcat的報告...

05-23 02:53:00.661: D/AndroidRuntime(17859): Shutting down VM 
05-23 02:53:00.671: W/dalvikvm(17859): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0) 
05-23 02:53:00.681: E/AndroidRuntime(17859): FATAL EXCEPTION: main 
05-23 02:53:00.681: E/AndroidRuntime(17859): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.HelloWorld.test/com.HelloWorld.test.HelloWorldActivity}: java.lang.NullPointerException 
05-23 02:53:00.681: E/AndroidRuntime(17859): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872) 
05-23 02:53:00.681: E/AndroidRuntime(17859): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893) 
05-23 02:53:00.681: E/AndroidRuntime(17859): at android.app.ActivityThread.access$1500(ActivityThread.java:135) 
05-23 02:53:00.681: E/AndroidRuntime(17859): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054) 
05-23 02:53:00.681: E/AndroidRuntime(17859): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-23 02:53:00.681: E/AndroidRuntime(17859): at android.os.Looper.loop(Looper.java:150) 
05-23 02:53:00.681: E/AndroidRuntime(17859): at android.app.ActivityThread.main(ActivityThread.java:4385) 
05-23 02:53:00.681: E/AndroidRuntime(17859): at java.lang.reflect.Method.invokeNative(Native Method) 
05-23 02:53:00.681: E/AndroidRuntime(17859): at java.lang.reflect.Method.invoke(Method.java:507) 
05-23 02:53:00.681: E/AndroidRuntime(17859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
05-23 02:53:00.681: E/AndroidRuntime(17859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 
05-23 02:53:00.681: E/AndroidRuntime(17859): at dalvik.system.NativeStart.main(Native Method) 
05-23 02:53:00.681: E/AndroidRuntime(17859): Caused by: java.lang.NullPointerException 
05-23 02:53:00.681: E/AndroidRuntime(17859): at com.HelloWorld.test.HelloWorldActivity.onCreate(HelloWorldActivity.java:20) 
05-23 02:53:00.681: E/AndroidRuntime(17859): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) 
05-23 02:53:00.681: E/AndroidRuntime(17859): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836) 
05-23 02:53:00.681: E/AndroidRuntime(17859): ... 11 more 
05-23 02:53:02.883: D/Process(17859): killProcess, pid=17859 

任何和所有的幫助是高度讚賞。

-Adrian

+0

的'NullPointerException'似乎是在這條線:'btn.setOnClickListener(本);'。你需要找出爲什麼'btn'爲空。 –

+1

Eclipse顯示沒有錯誤,因爲您遇到RuntimeException ...不是被編譯器捕獲的東西。 –

回答

0

試試這個,

只使用super.onCreate(冰柱)後的setContentView(R.layout.main),然後其他的事情。你應該總是先設置佈局,然後在其中執行視圖的工作。

例如:

public void onCreate(Bundle icicle){ 

     super.onCreate(icicle); 
     setContentView(R.layout.main); 

     ET = (EditText) findViewById(R.id.ET); 
     btn = (Button) findViewById(R.id.btn); 

     btn.setOnClickListener(this); 
     updateTime(); 



} 
+0

工作正常!我簡直不敢相信那是那麼簡單...... GRRR。 謝謝! – user1410773

+0

不客氣 –

+0

我不應該問這個,但我會得到這個正確的答案點? –