变量定义中带有findViewById的NullPointerException [英] NullPointerException with findViewById in variable definition

查看:73
本文介绍了变量定义中带有findViewById的NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关Null Point异常的非常流行的主题,但是对于为什么会收到错误,我仍然感到困惑.我正在尝试确保某些EditText已填写.我已经在onCreate方法中尝试过,但仍然无法正常工作.任何帮助,将不胜感激.抱歉,这是一个愚蠢的问题.

 公共类MainActivity扩展了AppCompatActivity实现的View.OnClickListener {EditText scoutName =(EditText)findViewById(R.id.editSN);EditText teamNumber =(EditText)findViewById(R.id.editTN);EditText matchNumber =(EditText)findViewById(R.id.editMatchNumber);@Override受保护的void onCreate(Bundle savedInstanceState){stringBuilder = new StringBuilder();super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);按钮提交= findViewById(R.id.submit);Submit.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){如果(fieldsOK){openActivity2();}别的 {System.out.println(您的表格遗失");}}});}私人布尔验证(EditText []字段){for(int i = 0; i< fields.length; i ++){EditText currentField = fields [i];if(currentField.getText().toString().length()< = 0){返回false;}}返回true;}boolean fieldsOK = validate(new EditText [] {scoutName,teamNumber,matchNumber}); 

错误

  java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.scoutingapp3250/com.example.scoutingapp3250.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()',引用为空对象 

错误出现在onCreate方法上方的scoutName上

解决方案

onCreate 中调用 setContentView 后,您需要调用 findViewById >

I have read the immensely popular thread about Null Point Exceptions but I'm still confused about why I'm getting the error. I am attempting to make sure some EditText's are filled out. I have tried it in the onCreate method and it still doesnt work. Any help would be appreciated. I apologize if this is some stupid question.

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

EditText scoutName = (EditText) findViewById(R.id.editSN);
EditText teamNumber = (EditText) findViewById(R.id.editTN);
EditText matchNumber = (EditText) findViewById(R.id.editMatchNumber);

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

Button submit = findViewById(R.id.submit);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (fieldsOK) {
                openActivity2();
            }
            else {
                System.out.println("You have Missing Forms");
            }
        }
    });
}


private boolean validate(EditText[] fields){
    for(int i = 0; i < fields.length; i++){
        EditText currentField = fields[i];
        if(currentField.getText().toString().length() <= 0){
            return false;
        }
    }
    return true;
}

boolean fieldsOK = validate(new EditText[] { scoutName, teamNumber, matchNumber });

error

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.scoutingapp3250/com.example.scoutingapp3250.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference

The error comes on the the scoutName on above the onCreate method

解决方案

You need to call findViewById after calling setContentView in onCreate

这篇关于变量定义中带有findViewById的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆