Eclipse没有看到错误...... Android强制关闭

时间:2012-05-22 17:01:56

标签: java eclipse

我尝试创建一个HelloWorld应用程序,当单击一个按钮时,该应用程序将使用当前时间更新文本字段。它看起来很好(eclipse发现没有错误),但每次我运行它时,我的手机都会崩溃......为什么?!?!?!

总之...

这是.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

1 个答案:

答案 0 :(得分: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();



}