java.lang.NullPointerException: Attempt to invoke virtual method android

后端 未结 2 655
甜味超标
甜味超标 2021-01-07 01:56

i am a beginner in the android, and i dont know what is the problem in my code ! and i need your help, this is my code: i am trying to do a calculator = - / * ..... and i re

相关标签:
2条回答
  • 2021-01-07 02:34

    Either check you have initialized the view or may be layout not has been attached to activity.

        password = (EditText) findViewById(R.id.password);
    

    or

    setContentView(R.layour.login_screen);
    
    0 讨论(0)
  • 2021-01-07 02:50

    You are initializing a local copy of variable theresult in onCreate:

    TextView theresult = (TextView) findViewById(R.id.TheResult);
    

    But in onClick you are trying to access another instance of the variable theresult which is declared outside onCreate. This one has not been initialized yet. That's why giving its giving null when you are trying to do setText.

    To solve, replace the below line:

    TextView theresult = (TextView) findViewById(R.id.TheResult);
    

    with

    theresult = (TextView) findViewById(R.id.TheResult);
    
    0 讨论(0)
提交回复
热议问题