findViewById returns null for EditText

findViewById returns null for EditText

Java Code:

public class MainActivity extends Activity {
    private EditText editText;
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        editText = (EditText) findViewById(R.id.etext);
        if (editText == null) {
            Log.v("editText", "booohooo");
        } else {
            Log.v("editText", "Success");
        }

        final Button button = (Button) findViewById(R.id.gobutton);
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if (editText != null) {
                    Log.v("editText", "is not NULL");
                } else {
                    Log.v("editText", "is NULL :(");
                }

                // Perform action on click
                if (editText != null) {
                    editText.getText();
                } else {
                    Log.v("editText", "is NULL");
                }
                Log.v("url", editText.getText().toString().trim());
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(editText.getText().toString().trim()));
                startActivity(browserIntent);
            }
        });
    }
}

Xml Code

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

    <TextView
        android_id="@+id/websiteurlheading"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Enter web site URL" />

    <EditText
        android_id="@+id/etext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/websiteurlheading" />

    <Button
        android:id="@+id/gobutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter" />
</LinearLayout>

Any help is appreciated.


Make sure that setContentView(R.layout.main); is set to the correctly layout. If you have made a new one (which includes that xml code above) then use that to set the content view - setContentView(R.layout.your_xml_filename);


change thes lines in your Views

android_id="@+id/websiteurlheading"

android_id="@+id/etext"

and change View id Like this

android:id="@+id/websiteurlheading" 
 android:id="@+id/etext"

eg

<TextView android:id="@+id/websiteurlheading"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Enter web site URL"
    />
<EditText android:id="@+id/etext"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_below="@id/websiteurlheading"
    />

I was facing this issue just a few seconds back and I had searched a few Stackoverflow posts. I found the solution myself though.

Just make sure you are not calling editText.getText().toString(); in onCreate method. Because it'll return the pre-loaded values only Hence editext will always return null in this case.

链接地址: http://www.djcxy.com/p/24206.html

上一篇: 在EditText中,点击特定的文本子集时弹出一个对话框

下一篇: findViewById为EditText返回null