问题描述:

I have these codes for my android activity. But it gave me java.lang.outofmemoryerror: java.lang.string[] of length 2131492864 ecxeeds the VM limit. I want to know if there is something wrong with my codes or is it my String.xml contains too much items? Or there's too many pictures? I have around 120 150x150 image that is around 20kb each. If possible I would like to know how can i solve this issue. Thanks in advance.

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

}

}

These are the error that shows on the catlog:

09-26 06:09:38.869: E/AndroidRuntime(2653): FATAL EXCEPTION: main

09-26 06:09:38.869: E/AndroidRuntime(2653): Process: com.example.loldb, PID: 2653

09-26 06:09:38.869: E/AndroidRuntime(2653): java.lang.OutOfMemoryError: java.lang.String[] of length 2131492864 exceeds the VM limit

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): at android.app.Activity.performCreate(Activity.java:5231)

09-26 06:09:38.869: E/AndroidRuntime(2653): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

09-26 06:09:38.869: E/AndroidRuntime(2653): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

09-26 06:09:38.869: E/AndroidRuntime(2653): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

09-26 06:09:38.869: E/AndroidRuntime(2653): at android.app.ActivityThread.access$800(ActivityThread.java:135)

09-26 06:09:38.869: E/AndroidRuntime(2653): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)

09-26 06:09:38.869: E/AndroidRuntime(2653): at android.os.Handler.dispatchMessage(Handler.java:102)

09-26 06:09:38.869: E/AndroidRuntime(2653): at android.os.Looper.loop(Looper.java:136)

09-26 06:09:38.869: E/AndroidRuntime(2653): at 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)

网友答案:

This line doesn't make much sense:

String[] list = new String[R.array.championlist];

R.array.championlist is an integer that points to the what I assume is a string array resource. You can load that string array with Resources.getStringArray(int).

String[] list = resources.getStringArray(R.array.championlist);
相关阅读:
Top