这是一个在android中的简单应用程序。它在执行时显示nullpointerexception
。 我无法从edittext
获得值。在这里,我只想从edittext
获得值。 但它显示nullpointerexception
..如何解决android中的NullPointer异常?
如何解决这个问题?
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;
}
}
请出示您的logcat –
找到它允许您从logcat的错误的行。 –
它没有显示logcat。显示一个消息框,运行该应用程序。 – AndRaGhu