Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference

前端 未结 3 504
广开言路
广开言路 2020-12-09 08:48

I try to use a fragment to open a database, however, when I click the button to begin searching, the program unexpectedly terminates and it show the error like this:

3条回答
  •  北海茫月
    2020-12-09 09:25

    I had this issue in my android activity.The mistake was my Toast was not getting the context or I would prefer to call it reference of the activity.

    Previous code:-

    Toast.makeText(context, "Session Expire..!", Toast.LENGTH_SHORT).show();
    

    Corrected Code:-

    Toast.makeText(activityname.this, "Session Expire..!", Toast.LENGTH_SHORT).show();
    

提交回复
热议问题