0

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 still can't get the getImage method to run properly because the getResources.getIdentifier line does not return anything since Context is null. I appreciated it if someone can teach me how come this is happening and how can I fix it. Thanks.

public class ChampionInfo extends FragmentActivity {

GridView gridtable;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_champion_info);

    gridtable = (GridView) findViewById(R.id.gridtable);        
    gridtable.setAdapter(new GridAdapter(getApplicationContext()));

}

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

}

}

14
  • Try to replace this code : gridtable.setAdapter(new GridAdapter(this)); Commented Sep 27, 2014 at 4:57
  • use gridtable.setAdapter(new GridAdapter(ChampionInfo.this)); and read stackoverflow.com/questions/7298731/… Commented Sep 27, 2014 at 4:57
  • mContext is still null. Commented Sep 27, 2014 at 5:02
  • @Raghunandan Since the OP is doing this in onCreate(), not an inner class, just this is completely fine. Commented Sep 27, 2014 at 5:11
  • @Code-Apprentice yes this will also work fine. Commented Sep 27, 2014 at 5:12

5 Answers 5

1

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

}
}
Sign up to request clarification or add additional context in comments.

Comments

1
private Context context;
   private ProgressDialog progressDialog;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_vehicle_details);

    context = this;
 }

Comments

0

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.

Comments

0

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;

}

Comments

0

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

1 Comment

This isn't the code that is getting null pointer exception. The codes simply runs, it doesn't crash or give error. But the mContext is just null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.