Reputation: 99
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
}
}
Upvotes: 0
Views: 5009
Reputation: 970
private Context context;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vehicle_details);
context = this;
}
Upvotes: 1
Reputation: 2877
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
Upvotes: 0
Reputation: 83527
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.
Upvotes: 0
Reputation: 93
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
}
}
Upvotes: 1
Reputation: 546
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;
}
Upvotes: 0