5

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!

Community
  • 1
  • 1
Mario
  • 353
  • 1
  • 5
  • 17
  • 1
    Can you post the output from the Logcat regarding the crash? It should begin with `E/` and contain the phrase `caused by`. And is your emulator also running 7.0? – Michael Dodd Mar 30 '17 at 13:11
  • Make sure you are using the latest version of SDK Tools package, and if the problem persists, please post the version of the tools. – John Joe Mar 30 '17 at 13:11
  • The debug console is indeed not interesting, what you want is the logcat output, found in the "Android monitor" tab in the bottom of Android Studio. Go look for an exception there. – JHH Mar 30 '17 at 13:14
  • @Mario Do you know what Logcat is and how you use it to find the stacktrace? If no, google logcat and trigger the crash and add the stacktrace from Logcat to the question – Zoe Mar 30 '17 at 13:30
  • @MichaelDodd I've posted the lines of the Logcat in my next answer. I think it depends on the Android version, but on a lower version API 22 it also doesn't seem to work accurate. – Mario Mar 30 '17 at 13:35
  • The problem is with the `ImageButton` `@id/btnSignIn` in your layout. What happens when you remove the lines `android:background="@null"` and `android:fontFamily="Montserrat"`? – Michael Dodd Mar 30 '17 at 13:41
  • Actually, scratch that. Go to file explorer, can you tell me how big `@drawable/loginbutton` is in KB? – Michael Dodd Mar 30 '17 at 13:44
  • @MichaelDodd Lognbutton is 240 x 240 px (53,2 kb) – Mario Mar 30 '17 at 13:47
  • And that is the only version of `loginbutton` you have in resources? Do you have any `hdpi` or `xhdpi` buckets in `res`? – Michael Dodd Mar 30 '17 at 13:48
  • @MichaelDodd Yes, this is the only version. Should I have more versions? I've now tinified every image i use. Now the button is 2,3 kb, but this didn't fix it. The only hdpi or xhdpi files are the original files inside the ic_launcher.png folder in minimap – Mario Mar 30 '17 at 13:50
  • Try removing the `src` property temporarily - does that stop the app from crashing? – Michael Dodd Mar 30 '17 at 13:52
  • Also, how big is `@drawable/loginbg` in KB? – Michael Dodd Mar 30 '17 at 13:56
  • It is long time you yourself mention that you have an OutOfMemory error. You can remove all code and still will have the error. No need to post code for two activities. What is the resolution of @drawable/loginbg in pixels? – greenapps Mar 30 '17 at 13:56
  • App still crashes. Logcat's first line: java.lang.RuntimeException: Canvas: trying to draw too large(256128000bytes) bitmap. – Mario Mar 30 '17 at 13:57
  • @MichaelDodd Loginbg is 104kb. I've also have a bg on the RegisterActivity (56,5 kb) and a Profile Picture at UserAreaActivity (28,4 kb) – Mario Mar 30 '17 at 13:58
  • @greenapps 1500 x 2668 px is this too big? I've exported this straight from the UI program Sketch – Mario Mar 30 '17 at 13:59
  • @Mario Those dimensions are way too big. Drawables are inflated to bitmaps when rendered, which take up a lot of memory. Try to reduce the size down, or crop to just the part you want displayed on screen. – Michael Dodd Mar 30 '17 at 14:00
  • That probably is too big. But why are you not removing all src attributes as a test as was suggested earlier? – greenapps Mar 30 '17 at 14:00
  • @greenapps I thought I should only remove the src of the imagebutton. After removing every background the app loads. Which resolution would you suggest so I can still use the background images? – Mario Mar 30 '17 at 14:04
  • Make them smaller. Less resolution. – greenapps Mar 30 '17 at 14:04
  • Which resolution would you suggest so I can still use the background images? – Mario Mar 30 '17 at 14:05
  • @Mario your code should be perfectly fine now. Feel free to revert it to how it was, just scale down `loginbg`. – Michael Dodd Mar 30 '17 at 14:05
  • I have no idea. Please try for yourself. And listen to Michael ;-). – greenapps Mar 30 '17 at 14:05
  • Thanks a lot to everyone for your fast help! I will try to figure out what resolution should be fine. – Mario Mar 30 '17 at 14:09
  • Try extend LoginActivity from AppCompatActivity – Andrii G Mar 30 '17 at 13:19

4 Answers4

3

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

Community
  • 1
  • 1
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • I've used the suggestions from this [link](http://stackoverflow.com/questions/13024272/android-background-image-size-in-pixel-which-support-all-devices) and now it works like a charm! – Mario Mar 30 '17 at 14:30
  • I have one last question: I am using this requestWindowFeature(Window.FEATURE_NO_TITLE); to remove the title bar on my main activity. On the emulator it worked well, but on my S7 the title bar is still there. Do you know how to fix this? – Mario Mar 30 '17 at 14:31
0

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.

Walter Palladino
  • 479
  • 1
  • 3
  • 8
  • I didn't knew that android devices would be so prone to file sizes in mid KB ranges. All in all my backgrounds had a size of about 400 kb in total, but that was enough for the app to crash. – Mario Mar 30 '17 at 14:41
  • I found myself working with Samsung devices which custom Android KitKat version did not implement correctly the garbage collection. – Walter Palladino Mar 31 '17 at 11:01
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

0

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

Arsalan Arain
  • 11
  • 1
  • 3