Android Activity Context is Null

后端 未结 5 1139
天涯浪人
天涯浪人 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

    0 讨论(0)
  • 2021-01-27 09:58
    private Context context;
       private ProgressDialog progressDialog;
    
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_vehicle_details);
    
        context = this;
     }
    
    0 讨论(0)
  • 2021-01-27 10:02

    Try below code or use getFragmentManager() or getParent();

    public class ChampionInfo extends FragmentActivity {
    
    GridView gridtable;
    
    Context ctx;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_champion_info);
        ctx=ChampionInfo.this;//getFragmentManager() or getParent()
        gridtable = (GridView) findViewById(R.id.gridtable);        
        gridtable.setAdapter(new GridAdapter(ctx));
    
    }
    
    public class GridAdapter extends BaseAdapter {
        private Context mContext;
    
        String[] list = getResources().getStringArray(R.array.championlist);
    
        int[] champImage = getImage();
    
        public int[] getImage() {
    
            int[] tempImage = new int[list.length];
    
            for (int i = 0; i < list.length; i++) {
                tempImage[i] = getResources().getIdentifier(list[i],
                        "drawable", getPackageName());
            }
    
            return tempImage;
    
        }
    
        // Constructor
        public GridAdapter(Context c) {
    
            mContext = c;
    
        }
    
        public int getCount() {
            return champImage.length;
        }
    
        public Object getItem(int position) {
            return null;
        }
    
        public long getItemId(int position) {
            return 0;
        }
    
        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
    
            } else {
                imageView = (ImageView) convertView;
            }
    
            imageView.setImageResource(champImage[position]);
            return imageView;
        }
    
        // Keep all Images in array
    
    }
    }
    
    0 讨论(0)
  • 2021-01-27 10:05

    I think mContext is not null, but the getResources() method return null.

    Change your code like below and try again, if it doesn't work. Paste the Logcat and we can debug the code.

    String[] list = mContext.getResources().getStringArray(R.array.championlist);
    

    and:

    public int[] getImage() {
    
        int[] tempImage = new int[list.length];
    
        for (int i = 0; i < list.length; i++) {
            tempImage[i] = mContext.getResources().getIdentifier(list[i],
                    "drawable", getPackageName());
        }
    
        return tempImage;
    
    }
    
    0 讨论(0)
  • 2021-01-27 10:06

    The root of your problem is that you are trying to initialize a member variable inline with

    int[] champImage = getImage();
    

    This executes before the constructor which sets mContext to a valid value. However, the fact that mContext is null inside the call for getImage() should be irrelevant because you never actually use mContext in that method.

    The overall issue is that you need to be careful about the order of execution.

    0 讨论(0)
提交回复
热议问题