[英]findViewById returns null for EditText
findViewById
returns null for EditText
findViewById
为EditText
返回null
Java Code: Java代码:
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代码
<?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);
确保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);
如果您创建了一个新的(包括上面的xml代码),那么使用它来设置内容视图 - 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 和更改视图ID像这样
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. 几秒钟后我就遇到了这个问题,我搜索了一些Stackoverflow帖子。 I found the solution myself though. 我自己找到了解决方案。
Just make sure you are not calling editText.getText().toString();
只要确保你没有调用editText.getText().toString();
in onCreate method. 在onCreate方法中。 Because it'll return the pre-loaded values only Hence editext will always return null in this case. 因为它只返回预加载的值因此在这种情况下editext将始终返回null。
如果用于setContentView函数的布局与EditBox放置布局不同,则会发生这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.