开发者

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

开发者 https://www.devze.com 2023-03-22 13:07 出处:网络
So I just started learning Android and I am reading the commonsware android development guide as a starting point. So basically I tried to create a view using xml and this xml is located in

So I just started learning Android and I am reading the commonsware android development guide as a starting point. So basically I tried to create a view using xml and this xml is located in

res/layout/edittext.xml

and the xml looks l开发者_JAVA百科ike:

<?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"/>

and my code

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());
    }
}

and the editText returns null... This is R class if it matters.

/* 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;
    }
}

stack trace

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)


In your onCreate() method, make sure you call setContentView() before any other UI-related tasks. i.e. most people call it right after super.onCreate()

Also, it looks like you have some misconceptions about layouts. You should normally have an encompassing "layout" object like a LinearLayout that you place View objects into, such as your EditText. The variable passed to setContentView() will be R.layout.name of your xml file.


You need to change setContentView(R.layout.main); to setContentView(R.layout.edittext);, and you you need to call it before you try to access the EditText


You have to setContentView before you try and edit the text in the editText.

setContentView(R.layout.main);

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

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

精彩评论

暂无评论...
验证码 换一张
取 消