Java 调用findViewById()后的EditText null

Java 调用findViewById()后的EditText null,java,android,xml,Java,Android,Xml,在过去的几天里,我一直有这个问题,但我似乎不知道出了什么问题。我觉得我已经把这些代码写了一百万遍了。我有两个EditText组件,我试图在单击按钮时从中获取文本。但由于某种原因,我得到了两个EditText的NullPointer异常。调用findViewById()会发现按钮很好,但似乎无法解析EditText 当我运行应用程序时,会出现UI,我可以单击“连接”按钮。Logcat告诉我NullPointerException来自“MainActivity.java”中的第46行,这是第一个Ed

在过去的几天里,我一直有这个问题,但我似乎不知道出了什么问题。我觉得我已经把这些代码写了一百万遍了。我有两个EditText组件,我试图在单击按钮时从中获取文本。但由于某种原因,我得到了两个EditText的NullPointer异常。调用findViewById()会发现按钮很好,但似乎无法解析EditText

当我运行应用程序时,会出现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>

也可用于其他编辑文本。

而不是:

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

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


您的手机似乎还可以。。您的logcat在哪里?您使用的是
android:label=“@+id/port\u输入”
,而不是
android:id=“@+id/port\u输入”“
etcAny投票失败的原因是什么?我想看看问题出在哪里…-我是个白痴。谢谢你,只是需要一双新的眼睛。我的脑子一定是因为参加期末考试而发疯了。这种事时有发生。很高兴我能帮忙!我们都去过!我看得太多了,谢谢。不知道我为什么用这个标签。
<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"/>
<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>