[英]How to solve the NullPointer exception in android?
This is a simple app in android. 这是android中的简单应用。 It shows
nullpointerexception
while executing. 它在执行时显示
nullpointerexception
。 I can't get the value from the edittext
. 我无法从
edittext
获取值。 Here I just want to get the value from the edittext
. 在这里,我只想从
edittext
获取值。 But it shows nullpointerexception
.. 但是它显示了
nullpointerexception
..
how to solve this? 如何解决呢?
public class Calci extends Activity {
Button add;
String str1;
String str2;
EditText txt1;
EditText txt2;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calci);
add=(Button) findViewById(R.id.button1);
txt1=(EditText) findViewById(R.id.editText1);
str1=txt1.getText().toString();
txt2=(EditText) findViewById(R.id.editText2);
str2=txt2.getText().toString();
tv=(TextView) findViewById(R.id.textView4);
addition();
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addition();
}
});
}
private void addition(){
int res=0;
res=Integer.parseInt(str1)+Integer.parseInt(str2);
tv.setText(String.valueOf(res));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_calci, menu);
return true;
}
}
Just remove addition();
只需删除
addition();
from onCreate()
. 来自
onCreate()
。 As I have doubt when its first time called your EditTexts
are empty. 我怀疑第一次调用您的
EditTexts
是空的。
Also when you call addition()
method you have to get the text from your both EditTexts
. 同样,当您调用
addition()
方法时,也必须从两个EditTexts
获取文本。
And your method addition()
something like, 和你的方法
addition()
类似,
private void addition()
{
int res=0;
str1=txt1.getText().toString();
str2=txt2.getText().toString();
res=Integer.parseInt(str1)+Integer.parseInt(str2);
tv.setText(String.valueOf(res));
}
make sure you are setting default values for editText1
and editText2
in xml or if you want to access values enter by user then chnage your code as and remove addition() before setOnClickListener : 确保您在xml中设置了
editText1
和editText2
默认值,或者如果您要访问用户输入的值,请更改您的代码,并在setOnClickListener之前删除add():
// addition(); comment this line
add.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
str1=txt1.getText().toString();
str2=txt2.getText().toString();
addition();
}
});
Just replace the additiion() method in your oncreate() and change the method as like below 只需在oncreate()中替换additiion()方法并按如下所示更改方法
private void addition()
{
str1= str1==null?"0":str1;// do like this, so that if str1 returns null then it will be replaced by 0
str2= str2==null?"0":str2;
int res=0;
res=Integer.parseInt(str1)+Integer.parseInt(str2);
tv.setText(String.valueOf(res));
}
You do not call addition() this method here : 您不要在这里调用additional()这个方法:
str2=txt2.getText().toString();
tv=(TextView) findViewById(R.id.textView4);
//addition();
add.setOnClickListener(new View.OnClickListener()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.