gpt4 book ai didi

java - Android Activity 上下文为空

转载 作者:行者123 更新时间:2023-12-02 05:25:33 26 4
gpt4 key购买 nike

所以我这里有这些代码,它运行时不会崩溃。但是,当我将“this”传递到网格适配器时,mContext 为空。我尝试传递 getApplicationContext() 但仍然无法使 getImage 方法正常运行,因为 getResources.getIdentifier 行不会返回任何内容,因为 Context 为空。如果有人能教我为什么会发生这种情况以及如何解决它,我将不胜感激。谢谢。

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

}

}

最佳答案

尝试下面的代码或使用getFragmentManager()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

}
}

关于java - Android Activity 上下文为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26071442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com