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

后端 未结 2 661
甜味超标
甜味超标 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: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);
    

提交回复
热议问题