Java (Android)findViewById无法找到视图并返回null?

Java (Android)findViewById无法找到视图并返回null?,java,android,Java,Android,所以我刚开始学习Android,我正在阅读Commonware Android开发指南作为起点。所以基本上我尝试使用xml创建一个视图,这个xml位于 res/layout/edittext.xml xml看起来像: <?xml version="1.0" encoding="utf-8" ?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:id="

所以我刚开始学习Android,我正在阅读Commonware Android开发指南作为起点。所以基本上我尝试使用xml创建一个视图,这个xml位于

res/layout/edittext.xml
xml看起来像:

<?xml version="1.0" encoding="utf-8" ?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/field"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:singleLine="false"/>
并且editText返回null。。。如果重要的话,这是R级

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.dennis;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class id {
        public static final int field=0x7f050000;
        public static final int helloText=0x7f050001;
    }
    public static final class layout {
        public static final int edittext=0x7f030000;
        public static final int main=0x7f030001;
    }
    public static final class string {
        public static final int app_name=0x7f040000;
    }
}
堆栈跟踪

07-22 11:30:46.670: ERROR/AndroidRuntime(674): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dennis/com.dennis.HelloWorld}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4627)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at com.dennis.HelloWorld.onCreate(HelloWorld.java:25)
在onCreate方法中,确保在调用任何其他与UI相关的任务之前调用setContentView。i、 大多数人在super.onCreate之后就叫它

另外,看起来您对布局有一些误解。通常,您应该有一个环绕布局对象,例如放置视图对象的线性布局,例如EditText。传递给setContentView的变量将是xml文件的R.layout.name。

您需要更改setContentViewR.layout.main;要设置ContentViewr.layout.edittext;,和你
您需要在尝试访问EditText之前调用它。在尝试编辑EditText中的文本之前,必须先设置ContentView

setContentView(R.layout.main);

EditText editText = (EditText) findViewById(R.id.field);

editText.setText("Hey there how are you doing");

如果您仍然有问题,请阅读。是的,非常感谢。我设置ContentViewr.layout.edittext,然后尝试访问R.id.field。好的,setContentView加载与R.layout.main关联的UI,edittext.setText尝试编辑主UI中的edittext字段,如果未加载UI,则无法真正编辑它。我想editText的声明实际上可以正常工作,引发异常的是editText.setText
setContentView(R.layout.main);

EditText editText = (EditText) findViewById(R.id.field);

editText.setText("Hey there how are you doing");