0

this is my first time in android studios and also anything related to coding since I took java in college. I'm currently following an easy youtube tutorial on how to make a simple app. Unfortunately whenever I run the AVD i get the "unfortunately your app has stopped" error. I have no idea what the problem is and I've been searching the internet and can't find an answer. I know its simple and you guys can probably find it quickly, so thats why I'm here. Thanks to whoever helps!

This is my main activity:

    package com.example.owner.pickupline;

    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Heres my code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight=".5">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/cleanImage"
            android:layout_centerVertical="true"
            android:src="@drawable/clean"
            android:nestedScrollingEnabled="false"
            android:cropToPadding="false"
            android:scaleType="fitXY"
            android:longClickable="false" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Clean"
            android:id="@+id/cleanButton"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:textSize="50dp"
            android:textColor="#ffffff"
            android:background="#a4000000"
            android:longClickable="true" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight=".5">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/dirtyImage"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:nestedScrollingEnabled="false"
            android:src="@drawable/dirty"
            android:scaleType="fitXY"
            android:longClickable="true" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Dirty"
            android:id="@+id/dirtyButton"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:textSize="50dp"
            android:textColor="#ffffff"
            android:background="#a4000000" />

    </RelativeLayout>


</LinearLayout>

Heres some of the error message

FATAL EXCEPTION: main
11-12 19:42:34.563 2376-2376/com.example.owner.pickupline E/AndroidRuntime: Process: com.example.owner.pickupline, PID: 2376
java.lang.OutOfMemoryError: Failed to allocate a 144000012 byte allocation with 1048576 free bytes and 63MB until OOM
11-12 19:42:34.563 2376-2376/com.example.owner.pickupline E/AndroidRuntime:     at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
11-12 19:42:34.563 2376-2376/com.example.owner.pickupline E/AndroidRuntime:     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
11-12 19:42:34.563 2376-2376/com.example.owner.pickupline E/AndroidRuntime:     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
11-12 19:42:34.563 2376-2376/com.example.owner.pickupline E/AndroidRuntime:     at 
  • Have you tried setting the AVD with more memory because that is the error you are receiving. I remember when I used Android Studio, the default maximum allotted memory for AVD was fairly low – Cup of Java Nov 13 '15 at 00:56
  • How much more memory would you suggest? I just added 3 GB and the same error is occurring. – Nick Dettore Nov 13 '15 at 01:05
  • To answer your question about how much memory, as much as it will take for it to run to your standards (probably no more than 2GB), and as for your other question, it doesn't seem like the error is caused by your code, it's just stating that it killed the main activity because it ran out of memory, try recompiling everything and restart AS. – Cup of Java Nov 13 '15 at 01:14
  • Are your image file(s) very large? – hichris123 Nov 13 '15 at 01:23
  • The image it's trying to load is 144MB in memory and that's too much. Images get uncompressed and each pixel uses 4 bytes. Just adding memory to the avd doesn't change that. Ideally you change the images to be small in memory. Or try setting the largeHeap flag http://stackoverflow.com/questions/11275650/how-to-increase-heap-size-of-an-android-application – zapl Nov 13 '15 at 01:49
  • Im almost positive you guys are right and it has something to do with the image size, but I used tiny pictures, and then no pictures at all and both times the AVD didnt change from "hello world." Thanks though, ill keep messing with it! – Nick Dettore Nov 13 '15 at 02:15

1 Answers1

0

Have you tried running on a real device, Most probable cause is because the AVD has default max heap size of 13mb which is very low comparing most devices, try changing that to 32mb. Window-->AVD Manager-->Virtual Devices-->Edit there you'll see the option to change max VM heap size. If it still not works try increasing more.

praveen.jar
  • 83
  • 1
  • 10