Android Activity Context is Null

后端 未结 5 1210
天涯浪人
天涯浪人 2021-01-27 09:49

So I have these Codes here, It runs without crashing. However When I pass \"this\" into the gridadapter the mContext is null. I tried to pass getApplicationContext() through but

5条回答
  •  情书的邮戳
    2021-01-27 09:53

    Try this code it will work

     Context mContext;
    
    GridView gridtable;
    
     @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_champion_info);
       mContext=this;
       gridtable = (GridView) findViewById(R.id.gridtable);        
       gridtable.setAdapter(new GridAdapter(mContext));
    
    }
    

    Edit1: Try getActivity() to get the context

提交回复
热议问题