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

}

}

user3370206
  • 79
  • 2
  • 7
  • Try to replace this code : gridtable.setAdapter(new GridAdapter(this)); – Haresh Chhelana Sep 27 '14 at 04:57
  • use `gridtable.setAdapter(new GridAdapter(ChampionInfo.this));` and read http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context – Raghunandan Sep 27 '14 at 04:57
  • mContext is still null. – user3370206 Sep 27 '14 at 05:02
  • @Raghunandan Since the OP is doing this in `onCreate()`, not an inner class, just `this` is completely fine. – Code-Apprentice Sep 27 '14 at 05:11
  • @Code-Apprentice yes `this` will also work fine. – Raghunandan Sep 27 '14 at 05:12
  • I originally had this in the parameter but mContext was null so thats why I was doing some trial and error but only putting this still make mContext null – user3370206 Sep 27 '14 at 05:13
  • @user3370206 Are you creating an instance of activity class? post the stack trace – Raghunandan Sep 27 '14 at 05:14
  • How do you know `mContext` is `null`? Specifically, which line is executed with `mContext` as a `null` value? (Note that `this` can **never** be `null`.) – Code-Apprentice Sep 27 '14 at 05:21
  • Well I put an if statement inside my getImage method to test is mContext is null and prints the result. – user3370206 Sep 27 '14 at 05:23
  • Btw the line that I need the context really badly is in the getImage method. When I write getResource().getIdentifier(....) the int will result in 0 – user3370206 Sep 27 '14 at 05:34
  • @Code-Apprentice Thanks for all your effort in helping me out, I finally figured out what's going on after I moved the for loop into the onCreate() so I can initialize my int[] first and pass it into the gridadapter constructor. You were right after the timing after all. Further more the reason why the tempImage was always empty regardless what we tried was being the array in String.xml did not match the name of the image file. If you can kindly post something on the answers regarding the timing. Well I can't +1 since I don't have enough rep but I will put it as the best answer. – user3370206 Sep 27 '14 at 06:35
  • "Well I put an if statement inside my getImage method to test is mContext is null and prints the result." Since `getImage()` never uses `mContext`, this is a non-issue. This happens, though, because you call `getImage()` when initializing `champImage` which executes before the constructor which sets the value of `mContext`. – Code-Apprentice Sep 27 '14 at 08:13
  • Answer added for your review. – Code-Apprentice Sep 27 '14 at 08:16
  • you can also use **ChampionInfo.this** or **ChampionInfo.this.getApplicationContext()** whereever you've use _mContext_ inside **GridAdapter** class – Kasim Rangwala Sep 27 '14 at 14:15

5 Answers5

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

}
}
1
private Context context;
   private ProgressDialog progressDialog;

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

    context = this;
 }
Kuldeep Singh
  • 823
  • 1
  • 8
  • 17
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.

Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226
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

Meenal
  • 2,854
  • 5
  • 17
  • 42
  • 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. – user3370206 Sep 27 '14 at 05:31
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;

}
zz-m
  • 346
  • 3
  • 12