我的android活动有这些代码。但它给了我java.lang.outofmemoryerror:长度为2131492864的java.lang.string []符合VM限制。我想知道我的代码是否有问题,或者我的String.xml包含太多项目?还是有太多的照片?我有大约120 150x150的图像,每个大约20kb。如果可能的话,我想知道如何解决这个问题。提前谢谢。
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(this));
}
public class GridAdapter extends BaseAdapter {
private Context mContext;
String[] list = new String[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
}
}
这些是catlog上显示的错误:
09-26 06:09:38.869:E / AndroidRuntime(2653):致命异常:主要 09-26 06:09:38.869:E / AndroidRuntime(2653):进程:com.example.loldb,PID:2653 09-26 06:09:38.869:E / AndroidRuntime(2653):java.lang.OutOfMemoryError:java.lang.String [],长度为2131492864,超过了VM限制 09-26 06:09:38.869:E / AndroidRuntime(2653):at com.example.loldb.ChampionInfo $ GridAdapter。(ChampionInfo.java:50) 09-26 06:09:38.869:E / AndroidRuntime(2653):at com.example.loldb.ChampionInfo.onCreate(ChampionInfo.java:44) 09-26 06:09:38.869:E / AndroidRuntime(2653):在android.app.Activity.performCreate(Activity.java:5231) 09-26 06:09:38.869:E / AndroidRuntime(2653):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 09-26 06:09:38.869:E / AndroidRuntime(2653):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 09-26 06:09:38.869:E / AndroidRuntime(2653):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 09-26 06:09:38.869:E / AndroidRuntime(2653):在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 09-26 06:09:38.869:E / AndroidRuntime(2653):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 09-26 06:09:38.869:E / AndroidRuntime(2653):在android.os.Handler.dispatchMessage(Handler.java:102) 09-26 06:09:38.869:E / AndroidRuntime(2653):在android.os.Looper.loop(Looper.java:136) 09-26 06:09:38.869:E / AndroidRuntime(2653):在android.app.ActivityThread.main(ActivityThread.java:5017) 09-26 06:09:38.869:E / AndroidRuntime(2653):at java.lang.reflect.Method.invokeNative(Native Method) 09-26 06:09:38.869:E / AndroidRuntime(2653):at java.lang.reflect.Method.invoke(Method.java:515) 09-26 06:09:38.869:E / AndroidRuntime(2653):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 09-26 06:09:38.869:E / AndroidRuntime(2653):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 09-26 06:09:38.869:E / AndroidRuntime(2653):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:4)
这条线没有多大意义:
String[] list = new String[R.array.championlist];
R.array.championlist是一个整数,指向我假设的字符串数组资源。您可以使用Resources.getStringArray(int)加载该字符串数组。
String[] list = resources.getStringArray(R.array.championlist);