Dusan Culum Dusan Culum - 9 days ago 5
Android Question

When the button is pressed the app crashes

I use android studio on my emulator applications work perfectly.

But when I started on real device (Samsung Galaxy S4 and Galaxy S6) application is launched but when I click the button it stops working.

Here is my code:

ImageView left, middle, right;
List<Integer> cards;
Button newGame;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

cards = new ArrayList<>();
cards.add(1); // 5 of diamonds
cards.add(2); // 7 of diamonds
cards.add(3); // 1 of spades

Collections.shuffle(cards);

newGame = (Button)findViewById(R.id.btnNewGame);
left = (ImageView) findViewById(R.id.ivCardLeft);
middle = (ImageView) findViewById(R.id.ivCardMiddle);
right = (ImageView) findViewById(R.id.ivCardRight);

newGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Collections.shuffle(cards);
left.setImageResource(R.drawable.cardback);
middle.setImageResource(R.drawable.cardback);
right.setImageResource(R.drawable.cardback);
Animation animLeft = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.left);
Animation animMiddle = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.middle);
Animation animRight = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.right);
left.startAnimation(animLeft);
middle.startAnimation(animMiddle);
right.startAnimation(animRight);

}
});

left.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cards.get(2) == 1) {
left.setImageResource(R.drawable.five);
} else if (cards.get(2) == 2) {
left.setImageResource(R.drawable.seven);
} else if (cards.get(2) == 3) {
left.setImageResource(R.drawable.ace);
Toast.makeText(MainActivity.this, "Guessed!", Toast.LENGTH_SHORT).show();

}
if (cards.get(1) == 1) {
middle.setImageResource(R.drawable.five);
} else if (cards.get(1) == 2) {
middle.setImageResource(R.drawable.seven);
} else if (cards.get(1) == 3) {
middle.setImageResource(R.drawable.ace);

}
if (cards.get(0) == 1) {
right.setImageResource(R.drawable.five);
} else if (cards.get(0) == 2) {
right.setImageResource(R.drawable.seven);
} else if (cards.get(0) == 3) {
right.setImageResource(R.drawable.ace);

}
}
});
middle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cards.get(2) == 1) {
left.setImageResource(R.drawable.five);
} else if (cards.get(2) == 2) {
left.setImageResource(R.drawable.seven);
} else if (cards.get(2) == 3) {
left.setImageResource(R.drawable.ace);


}
if (cards.get(1) == 1) {
middle.setImageResource(R.drawable.five);
} else if (cards.get(1) == 2) {
middle.setImageResource(R.drawable.seven);
} else if (cards.get(1) == 3) {
middle.setImageResource(R.drawable.ace);
Toast.makeText(MainActivity.this, "Guessed!", Toast.LENGTH_SHORT).show();

}
if (cards.get(0) == 1) {
right.setImageResource(R.drawable.five);
} else if (cards.get(0) == 2) {
right.setImageResource(R.drawable.seven);
} else if (cards.get(0) == 3) {
right.setImageResource(R.drawable.ace);

}
}
});
right.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cards.get(2) == 1) {
left.setImageResource(R.drawable.five);
} else if (cards.get(2) == 2) {
left.setImageResource(R.drawable.seven);
} else if (cards.get(2) == 3) {
left.setImageResource(R.drawable.ace);


}
if (cards.get(1) == 1) {
middle.setImageResource(R.drawable.five);
} else if (cards.get(1) == 2) {
middle.setImageResource(R.drawable.seven);
} else if (cards.get(1) == 3) {
middle.setImageResource(R.drawable.ace);

}
if (cards.get(0) == 1) {
right.setImageResource(R.drawable.five);
} else if (cards.get(0) == 2) {
right.setImageResource(R.drawable.seven);
} else if (cards.get(0) == 3) {
right.setImageResource(R.drawable.ace);
Toast.makeText(MainActivity.this, "Guessed!", Toast.LENGTH_SHORT).show();

}
}
});

}
}


My Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dusan.cupd">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


And my XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Background"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:gravity="center"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.dusan.cupd.MainActivity"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
android:gravity="center">

<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:src="@drawable/cardback"
android:layout_marginRight="60dp"
android:id="@+id/ivCardLeft" />

<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:src="@drawable/cardback"
android:id="@+id/ivCardMiddle" />

<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:src="@drawable/cardback"
android:layout_marginLeft="60dp"
android:id="@+id/ivCardRight" />
</LinearLayout>

<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:id="@+id/btnNewGame"
android:text="New Game"
android:backgroundTint="@android:color/darker_gray" />

</LinearLayout>


Button
new game
work, but game crashes when i click on any of Cards.(left,middle,right)

My Logcat :

11-27 13:42:46.754 30166-30166/com.example.dusan.cupd E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dusan.cupd, PID: 30166
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
at android.content.res.Resources.loadDrawable(Resources.java:3056)
at android.content.res.Resources.getDrawable(Resources.java:1613)
at android.support.v7.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:128)
at android.support.v7.widget.TintResources.getDrawable(TintResources.java:45)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:346)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:185)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:69)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:78)
at com.example.dusan.cupd.MainActivity$3.onClick(MainActivity.java:103)
at android.view.View.performClick(View.java:4633)
at android.view.View$PerformClick.run(View.java:19330)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)

Answer

first you should edit your question not adding an answer, second put this in application tag in your manifest file

android:largeHeap="true"