嗨我試圖創建一個Android應用程序來顯示圖像,以便您可以單擊「下一個」按鈕,然後下一個圖像顯示。到目前爲止,我有這個方法嘗試運行Android應用程序時獲取java.lang.OutOfMemoryError
public void buttonClick(){
imgView = (ImageView)findViewById(R.id.imgView);
buttonS = (Button)findViewById(R.id.button);
buttonS.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
current_image_index++;
current_image_index = current_image_index % images.length;
imgView.setImageResource(images[current_image_index]);
}
}
);
}
然而,當我嘗試運行此,我得到當我點擊「下一步」按鈕兩次一個java.lang.OutOfMemoryError。難道我做錯了什麼?我的圖像陣列看起來像這樣:
int[] images = {R.drawable.img};
我試圖改變清單文件中包括
android:largeHeap="true"
這是我設置的變量和撥打以上
public class MainActivity extends AppCompatActivity {
private static ImageView imgView;
private static Button buttonS;
private int current_image_index;
int[] images = {R.drawable.dog,R.drawable.img};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonClick();
}
的方法堆棧跟蹤是:
08-29 23:17:34.781 15450-15450/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.daniel.firstapp, PID: 15450
java.lang.OutOfMemoryError: Failed to allocate a 2070252108 byte allocation with 16777216 free bytes and 482MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:816)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:637)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1019)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:3778)
at android.content.res.Resources.loadDrawable(Resources.java:3651)
at android.content.res.Resources.getDrawable(Resources.java:1865)
at android.content.Context.getDrawable(Context.java:408)
at android.widget.ImageView.resolveUri(ImageView.java:752)
at android.widget.ImageView.setImageResource(ImageView.java:408)
at com.example.daniel.firstapp.MainActivity$1.onClick(MainActivity.java:40)
at android.view.View.performClick(View.java:5217)
at android.view.View$PerformClick.run(View.java:20983)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6141)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
你可以添加更多類的代碼,你可以調用這個方法,更重要的是添加堆棧跟蹤 –
確定很酷,現在就做到這一點。 – fosho
現在有一個新錯誤 – fosho