调用findViewById()后,EditText为null

时间:2014-12-23 04:59:39

标签: java android xml

过去几天我遇到过这个问题,我似乎无法弄清楚出了什么问题。我觉得我已经写了几百万次这些精确的代码行。我有两个EditText组件,我试图从单击按钮时获取文本。但由于某种原因,我得到了两个EditTexts的NullPointer异常。调用findViewById()可以很好地找到按钮,但似乎无法解析EditTexts。

当我运行应用程序时,会出现UI,我可以单击“连接”按钮。 Logcat告诉我NullPointerException来自“MainActivity.java”中的第46行,这是第一个EditText所在的位置。我已经运行测试并验证它们是空的。我似乎无法弄清楚为什么,任何有关解决问题的见解都会受到高度赞赏。我正在运行Android Studio v1.0.2。

MainActivity.java

public class MainActivity extends Activity{

private TextView _ipLabel;

private EditText _ipInput;
private EditText _portInput;

private Button _hostButton;
private Button _connectButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    _ipLabel = (TextView) findViewById(R.id.ip_label);

    _ipInput = (EditText) findViewById(R.id.ip_input);
    _portInput = (EditText) findViewById(R.id.port_input);

    _hostButton = (Button) findViewById(R.id.hostButton);
    _connectButton = (Button) findViewById(R.id.connectButton);

    _hostButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("Host Button", "Clicked");
        }
    });
    _connectButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        String connectionAddr = _ipInput.getText().toString() + ":";
        connectionAddr+= _portInput.getText().toString();
        Log.d("Host", connectionAddr);
        }
    });
}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
    android:id="@+id/ip_label"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="-.-.-.-"/>

<Button
    android:id="@+id/hostButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="   Host   "/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IP: "/>
    <EditText
        android:label="@+id/ip_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PORT: "/>
    <EditText
        android:label="@+id/port_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

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

3 个答案:

答案 0 :(得分:5)

<EditText
    android:label="@+id/port_input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

此处不是android:label,应该是android:id

将此更改为,

android:id="@+id/port_input"

还使用其他编辑文字。

答案 1 :(得分:1)

而不是:

<EditText
    android:label="@+id/ip_input" // <-- this should be an id not a label
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

使用:

<EditText
    android:id="@+id/ip_input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

答案 2 :(得分:0)

试试这个..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
    android:id="@+id/ip_label"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="-.-.-.-"/>

<Button
    android:id="@+id/hostButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="   Host   "/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IP: "/>
    <EditText
        android:id="@+id/ip_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PORT: "/>
    <EditText
        android:id="@+id/port_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

<Button
    android:id="@+id/connectButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Connect"/></LinearLayout>
相关问题