[英]NullPointerException on a setText Method
I'll first give you the Java- and XML- Code and explain my problem below. 首先,我将向您提供Java和XML代码,并在下面说明我的问题。
EditText et1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText et1 = (EditText) findViewById(R.id.et1);
}
//Called when Button clicked
public void calculate(View view) {
et1.setText("test"); //ERROR IN THIS LINE
}
(relevant) XML-Code (相关)XML代码
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/et2"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:inputType="numberDecimal"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnText"
android:id="@+id/btnCalc"
android:layout_below="@+id/et2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="49dp"
android:onClick="calculate"/>
As soon as I click on the button, my app closes. 单击按钮后,我的应用程序立即关闭。 In Android Studio I can see that there's a NullPointerExeption. 在Android Studio中,我可以看到有一个NullPointerExeption。 When i write "EditText et1 = (EditText) findViewById(R.id.et1);" 当我写“ EditText et1 =(EditText)findViewById(R.id.et1);”时 in the "calculate"-method everything works fine. 在“计算”方法中,一切正常。 So my questions are: 所以我的问题是:
Why? 为什么?
How to make it so that i can use the functions of the EditText et1 in every method (in this class)? 如何使它可以在每种方法(此类)中使用EditText et1的功能?
I know by reading other posts that findViewById(id) apperently gives back "null" but I used the right Id. 通过阅读其他帖子,我知道findViewById(id)会适当地返回“ null”,但我使用了正确的ID。
You are defining edit text two times, do this instead: 您要定义两次编辑文本,请执行以下操作:
EditText et1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.et1);
}
//Called when Button clicked
public void calculate(View view) {
et1.setText("test"); //ERROR IN THIS LINE
}
You dont have R.id.et1 your id in edit text is et2 您没有R.id.et1,您在编辑文本中的ID是et2
EditText et1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.et2); // change id
}
//Called when Button clicked
public void calculate(View view) {
et1.setText("test"); //ERROR IN THIS LINE
}
As Aakash and Dici wrote, you're re-defining EditText et1
inside of your onCreate()
method, instead of setting the attribute. 正如Aakash和Dici所写,您是在onCreate()
方法中重新定义EditText et1
,而不是设置属性。 Also, you're trying to set et1
to the view with the id R.id.et1
, the exception shall be triggered again as the ID you want to set it to is R.id.et2
. 另外,你要设置et1
到编号的观点R.id.et1
,异常应再次触发,只要你想将它设置为是ID R.id.et2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.