小编poi*_*our的帖子

android中的findViewById()的NullPointerException

在下面的代码中,我使用findViewById()在第9/10行获得NullPointerException.
在我的主类中,我只是从这个类中实例化一个对象,使用.getFrom()

public class UserInteraction extends Activity {
EditText etFrom;
int from;
EditText etTill;
int till;

public UserInteraction(){
    etFrom = (EditText)findViewById(R.id.et_from);
    etTill = (EditText)findViewById(R.id.et_till);
}

public int getFrom() {
    String s = etFrom.getText().toString();
    int i = Integer.parseInt(s);
    return i;
}

public int getTill() {
    String s = etTill.getText().toString();
    int i = Integer.parseInt(s);
    return i;
}
Run Code Online (Sandbox Code Playgroud)

是不是在我的主类中设置了contentView ..?可能是什么原因 ?

java android nullpointerexception android-edittext android-activity

7
推荐指数
1
解决办法
1万
查看次数