当我第二次 运行 相同的函数时,为什么我得到 null exceptionj?

Why I'm getting null exceptionj when I run the same function for the second time?

我有一个按钮,我通过它调用函数 click 当我第二次单击该按钮时,应用程序崩溃了。 这是因为我在空对象上调用了 setEdit。 除了第二种变体,还有其他方法可以解决问题吗 下面给出 if(!=null) ?有没有解释为什么我得到这个 null 异常?

public void click(View view) {

        EditText editText = (EditText)findViewById(R.id.simpleEditText);


    if (editText != null)  { editText.setId(202); }

}

但他的作品还不错:

public void click(View view) {

        EditText editText = (EditText)findViewById(R.id.simpleEditText);


    if (editText != null)  { editText.setId(202); }

}

这是对话内容,向他展示了如何获取视图并存储它们。

public class MyActivity extends Activity {

    private TextView text1;

    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.mylayout);
        text1 = findViewById(R.id.text);
    }

    private void someFunc() {
        text1.setText("Hey I used the variable");
    }
}