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

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

问题描述

我已经阅读了关于空点异常的非常流行的线程,但我仍然对为什么我收到错误感到困惑.我试图确保填写了一些 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);@覆盖protected void onCreate(Bundle savedInstanceState) {stringBuilder = new StringBuilder();super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);按钮提交 = findViewById(R.id.submit);submit.setOnClickListener(new View.OnClickListener() {@覆盖public void onClick(View v) {如果(字段确定){openActivity2();}别的 {System.out.println("您缺少表单");}}});}私有布尔验证(EditText[] 字段){for(int i = 0; i 

错误

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天全站免登陆