Mario
Mario

Reputation: 355

Android Studio app crashes on real device, but works on emulator

I started using Android Studio a few weeks ago. I've worked on an app with a normal login screen and on the emulator everything works fine. But if I try to run the app on my Samsung Galaxy S7 Edge (Android 7.0) the app crashes instantly.

I've emulated an Nexus 5 with API 23, when I try it with an emulated Pixel with API 24 the emulator also crashes. So I guess the problem depends on the Android version?

Logcat (I tinified all images I am using, now this is my new Logcat):

03-30 15:53:13.841 31645-31645/de.mm.uchron E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: de.mm.uchron, PID: 31645
                                                                      java.lang.OutOfMemoryError: Failed to allocate a 3686412 byte allocation with 2474312 free bytes and 2MB until OOM
                                                                          at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                          at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                          at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:700)
                                                                          at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:535)
                                                                          at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1179)
                                                                          at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:770)
                                                                          at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:621)
                                                                          at android.content.res.Resources.loadDrawable(Resources.java:1727)
                                                                          at android.content.res.TypedArray.getDrawable(TypedArray.java:945)
                                                                          at android.widget.ImageView.<init>(ImageView.java:157)
                                                                          at android.widget.ImageButton.<init>(ImageButton.java:85)
                                                                          at android.widget.ImageButton.<init>(ImageButton.java:81)
                                                                          at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:60)
                                                                          at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:56)
                                                                          at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:118)
                                                                          at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
                                                                          at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
                                                                          at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:378)
                                                                          at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                          at de.mm.uchron.LoginActivity.onCreate(LoginActivity.java:32)
                                                                          at android.app.Activity.performCreate(Activity.java:6912)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
                                                                          at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:154)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6688)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
03-30 15:53:20.016 31645-31656/de.mm.uchron I/art: Clamp target GC heap from 261MB to 256MB

I've also tried it with my another real device (Samsung GT-I9300 with Android 5.1.1, API 22). The MainActivity loads, but without the background image and if I try to login, the app crashes.

My SDK Tools have all the newest version

Android Emulator: 26.0.0
Android SDK Platform-Tools: 25.0.4
Android SDK Tools: 26.0.0
Google Play services, rev 39: 39.0.0
Google USB Driver, rev 11: 11.0.0
Google Web driver: 2
Intel x86 Emulator Accelerator (HAXM installer): 6.0.5

xml file for the MainActivity (LoginActivity):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:background="@drawable/loginbg">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/rect"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true">


    </LinearLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/signup_input_layout_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="300dp"
        android:layout_marginLeft="30dp">


    <EditText
        android:id="@+id/etUsername"
        android:layout_width="222dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textColor="#000"
        android:fontFamily="Montserrat"
        android:textSize="16dp"
        android:hint="BENUTZERNAME" />
    </android.support.design.widget.TextInputLayout>


    <android.support.design.widget.TextInputLayout
        android:id="@+id/signup_input_layout_pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_below="@id/signup_input_layout_name">

        <EditText
            android:id="@+id/etPassword"
            android:layout_width="223dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:fontFamily="Montserrat"
            android:hint="PASSWORT"
            android:inputType="textPassword"
            android:textColor="#000"
            android:textSize="16dp" />
    </android.support.design.widget.TextInputLayout>

    <ImageButton
        android:id="@+id/bSignIn"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="@null"
        android:layout_marginBottom="60dp"
        android:layout_marginLeft="260dp"
        android:fontFamily="Montserrat"
        android:scaleType="fitXY"
        android:src="@drawable/loginbutton"
        android:text="Login" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="NEUER ACCOUNT"
        android:id="@+id/tvRegisterLink"
        android:textSize="12sp"
        android:textColor="#B2B2B2"
        android:fontFamily="Montserrat"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:layout_alignStart="@+id/signup_input_layout_pass" />

</RelativeLayout>

Like I said I am new to Android Studio and I become exasperated with this problem.

Thanks in advance!

Solved: Resizing the images solved the problem. I used the suggestions from this link to reduce the resolution of my background files. Now everything works like a charm!

Upvotes: 5

Views: 16328

Answers (4)

Arsalan Arain
Arsalan Arain

Reputation: 11

you must have to check Api level of device or project or Real device first match the Api level then run.

Upvotes: 0

Go to Styles.xml and change the fisrt line to this,

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

this way no more title bar and you get full screen

Upvotes: 0

Walter Palladino
Walter Palladino

Reputation: 479

Agree with Dod answer but I want to extend it a bit. If you don't need transparency, use JPG and when you create Bitmaps, choose option which do not require transparencies and even lesser bit count.

Upvotes: 0

Michael Dodd
Michael Dodd

Reputation: 10270

You mention that you're using an image with dimensions 1500 x 2668 in this comment. As shown in the logcat, Android is using BitmapFactory to inflate this image, and Bitmaps take up memory based on image dimensions. In your case this is a lot of memory, more than the device has available for your app, thus the OutOfMemoryException.

Scale the dimensions of @drawable/loginbg down as far as you can without compromising quality. I'd recommend reading about how to support multiple screen sizes using images of different dimensions: https://developer.android.com/guide/practices/screens_support.html

Upvotes: 3

Related Questions