0

我尝试创建一个 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

任何和所有的帮助都非常感谢。

-阿德里安

4

1 回答 1

0

试试这个,

在 super.onCreate(icicle) 之后使用 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();



}
于 2012-05-22T17:17:31.520 回答