Java 调用GetText()时出现NullPointerException

Java 调用GetText()时出现NullPointerException,java,android,Java,Android,首先,我是一个Android noob,我正在制作一个简单的应用程序,它从EditText小部件中获取文本,并在点击按钮时在TextView中显示。但是当点击按钮时,它会强制关闭,我在logcat中得到一个NullPointerException 我的活动代码是: package com.deltablue.freeearl.ama.ama; import android.app.Activity; import android.os.Bundle; import android.view.V

首先,我是一个Android noob,我正在制作一个简单的应用程序,它从
EditText
小部件中获取文本,并在点击按钮时在
TextView
中显示。但是当点击按钮时,它会强制关闭,我在logcat中得到一个
NullPointerException

我的活动代码是:

package com.deltablue.freeearl.ama.ama;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {

    private Button b1;
    private EditText e1;
    private TextView t1;

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

        //Definiciones de los elementos
        Button b1 = (Button) findViewById(R.id.bttn1);
        EditText e1 = (EditText) findViewById(R.id.edit1);
        TextView t1 = (TextView) findViewById(R.id.txt1);
    }

    public void b1click(View view) {
        Toast.makeText(this, "Pressed", Toast.LENGTH_SHORT).show();
        String text = e1.getText().toString();
        t1.setText("Input: ");
    }
}
问题是,在调试时,
findviewbyd()
调用返回正确的值。但是当执行
b1click()
时,
e1
t1
为空。我看到的所有其他类似问题都必须通过
findviewbyd()
返回null或错误的小部件ID来解决,但这不是我的情况,好吧。我知道这可能是一个非常愚蠢的错误,但我不知道我错在哪里

我的布局是:

<RelativeLayout 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"
    tools:context="com.deltablue.freeearl.ama.ama.MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show text"
            android:id="@+id/bttn1"
            android:layout_gravity="center_horizontal"
            android:onClick="b1click" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/txt1"
            android:editable="true" />
    </LinearLayout>
</RelativeLayout>


提前感谢您的帮助

此处,在
onCreate
中:

Button b1 = (Button) findViewById(R.id.bttn1);
EditText e1 = (EditText) findViewById(R.id.edit1);
TextView t1 = (TextView) findViewById(R.id.txt1);
。。。您正在声明三个局部变量。这些字段与前面声明的字段完全不同:

private Button b1;
private EditText e1;
private TextView t1;
我很确定您只是想更改字段的值,所以您只想为变量分配新值,而不是重新定义它们:

b1 = (Button) findViewById(R.id.bttn1);
e1 = (EditText) findViewById(R.id.edit1);
t1 = (TextView) findViewById(R.id.txt1);

这里,在
onCreate
中:

Button b1 = (Button) findViewById(R.id.bttn1);
EditText e1 = (EditText) findViewById(R.id.edit1);
TextView t1 = (TextView) findViewById(R.id.txt1);
。。。您正在声明三个局部变量。这些字段与前面声明的字段完全不同:

private Button b1;
private EditText e1;
private TextView t1;
我很确定您只是想更改字段的值,所以您只想为变量分配新值,而不是重新定义它们:

b1 = (Button) findViewById(R.id.bttn1);
e1 = (EditText) findViewById(R.id.edit1);
t1 = (TextView) findViewById(R.id.txt1);

@Jens:我认为在这种情况下没有必要这样做-OP已经诊断出
e1
t1
为空的问题。问题是为什么它们是空的,堆栈跟踪对此没有帮助。@Jens:我认为在这种情况下没有必要这样做-OP已经诊断出
e1
t1
是空的问题。问题是为什么它们是空的,而堆栈跟踪对此没有帮助。天哪,我知道这是非常愚蠢的事情。我很尴尬,哈哈。谢谢,问题解决了!我会尽快接受你的回答。天哪,我就知道这是件很愚蠢的事。我很尴尬,哈哈。谢谢,问题解决了!我会尽快接受你的答复。