繁体   English   中英

(Android) findViewById 定位视图失败,返回null?

[英](Android) findViewById Fails to locate the view and returns null?

所以我刚开始学习 Android,我正在阅读 commonsware 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"/>

和我的代码

public class HelloWorld extends Activity implements View.OnClickListener {

    Button btn;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("HEY");

        btn = new Button(this);
        EditText editText = (EditText) findViewById(R.id.field);

        editText.setText("Hey there how are you doing");
        updateTime();
        setContentView(R.layout.main);
    }

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

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

并且editText返回null ...如果重要的话,这是R class。

/* 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() 即大多数人在super.onCreate()之后立即调用它

此外,您似乎对布局有一些误解。 您通常应该有一个包含“布局” object ,例如放置View对象的LinearLayout ,例如EditText 传递给setContentView()的变量将是 R.layout。 您的 xml 文件的名称

您需要更改setContentView(R.layout.main); 设置setContentView(R.layout.edittext); ,并且您需要在尝试访问 EditText 之前调用它

在尝试编辑editText 中的文本之前,您必须先设置ContentView。

setContentView(R.layout.main);

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM