App crashes when I open another activity
Asked Answered
R

1

0

So I am creating an app with 5 activities that have 5 separate soundBoards in which are controlled by 5 separate soundPools. I am also using a navigational drawer on each activity. When I run the app it can open the first two activities but whenever I call the third to open the app stops working. On the memory monitor the memory usage peaks to 64MB and then it crashes. Is it just the emulator is limited to 64MB and that it will work fine on a phone or do I have a memory leak somewhere? Do I need to call onStop when I switch to another activity and then onStart when its reopened or do I not need to? If I do how would I do that for this project?

Here is a link to view my crash log: https://www.dropbox.com/s/x5obsexzgi2ofaa/crashlog2.docx?dl=0

MainActivity.java class:

public class MainActivity extends ActionBarActivity {

DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
String[] mDrawerListItems;

SoundPool Ken;
int goodschoolId;
int challengeId;
int frontId;
int driverId;
int roadId;
int tiesId;
int noId;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Ken = new SoundPool(1, AudioManager.STREAM_MUSIC, 1);
   goodschoolId = Ken.load(this, R.raw.goodschool,1);
    challengeId = Ken.load(this, R.raw.challenge,1);
    frontId = Ken.load(this, R.raw.front,1);
   driverId = Ken.load(this, R.raw.driver,1);
    roadId = Ken.load(this, R.raw.road,1);
    tiesId = Ken.load(this, R.raw.ties,1);
    noId = Ken.load(this, R.raw.no,1);

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer);
    mDrawerList = (ListView)findViewById(android.R.id.list);
    mDrawerListItems = getResources().getStringArray(R.array.drawer_list);
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDrawerListItems));
    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch(position) {
                case 0:
                    Intent i = new Intent(MainActivity.this, MrsClubb.class);
                    startActivity(i);
                    break;
                case 1:
                    Intent ii = new Intent(MainActivity.this, MainActivity.class);
                    startActivity(ii);
                    break;
                case 2:
                    Intent iii = new Intent(MainActivity.this, MrSmith.class);
                    startActivity(iii);
                    break;
            }
            mDrawerLayout.closeDrawer(mDrawerList);

        }
    });
    mDrawerToggle = new ActionBarDrawerToggle(this,
            mDrawerLayout,
            toolbar,
            R.string.drawer_open,
            R.string.drawer_close){
        public void onDrawerClosed(View v){
            super.onDrawerClosed(v);
            invalidateOptionsMenu();
            syncState();
        }
        public void onDrawerOpened(View v){
            super.onDrawerOpened(v);
            invalidateOptionsMenu();
            syncState();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    mDrawerToggle.syncState();
}

@Override
protected void onPostCreate(Bundle savedInstanceState){
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
        case android.R.id.home: {
            if (mDrawerLayout.isDrawerOpen(mDrawerList)){
                mDrawerLayout.closeDrawer(mDrawerList);
            } else {
                mDrawerLayout.openDrawer(mDrawerList);
            }
            return true;
        }
        default: return super.onOptionsItemSelected(item);
    }
}

public void playSound11(View view) {Ken.play(goodschoolId, 1, 1, 1, 0, 1);}
public void playSound12(View view) {Ken.play(challengeId, 1, 1, 1, 0, 1);}
public void playSound13(View view) {Ken.play(frontId, 1, 1, 1, 0, 1);}
public void playSound14(View view) {Ken.play(driverId, 1, 1, 1, 0, 1);}
public void playSound15(View view) {Ken.play(roadId, 1, 1, 1, 0, 1);}
public void playSound16(View view) {Ken.play(tiesId, 1, 1, 1, 0, 1);}
public void playSound17(View view) {Ken.play(noId, 1, 1, 1, 0, 1);}

}

Code for the activity that crashes (pretty much the same as above just different sound pool and audio files called)

activitymain.xml:

<LinearLayout 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"
tools:context=".MyActivity"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/primary"
    app:theme="@style/ToolbarTheme"
    app:popupTheme="@style/Theme.AppCompat"/>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/kenned"
        >

        <Button
            android:layout_width="280dp"
            android:layout_height="60dp"
            android:onClick="playSound11"
            android:id="@+id/button1"
            android:background="@drawable/custombtn11"


            android:layout_marginTop="61dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true" />
        <Button
            android:layout_width="270dp"
            android:layout_height="40dp"
            android:background="@drawable/custombtn12"
            android:onClick="playSound12"
            android:id="@+id/button2"
            android:layout_below="@+id/button1"
            android:layout_alignParentEnd="true"

            />
        <Button
            android:layout_width="260dp"
            android:layout_height="39dp"
            android:background="@drawable/custombtn13"
            android:onClick="playSound13"
            android:id="@+id/button3"
            android:layout_below="@+id/button2"
            android:layout_alignParentEnd="true"

            />
        <Button
            android:layout_width="330dp"
            android:layout_height="65dp"
            android:background="@drawable/custombtn14"
            android:onClick="playSound14"
            android:id="@+id/button4"
            android:layout_below="@+id/button3"
            android:layout_alignParentEnd="true"

            />
        <Button
            android:layout_width="180dp"
            android:layout_height="40dp"
            android:background="@drawable/custombtn15"
            android:onClick="playSound15"
            android:id="@+id/button5"
            android:layout_below="@+id/button4"
            android:layout_alignParentEnd="true"

            />
        <Button
            android:layout_width="300dp"
            android:layout_height="65dp"
            android:background="@drawable/custombtn16"
            android:onClick="playSound16"
            android:id="@+id/button6"
            android:layout_below="@+id/button5"
            android:layout_alignParentEnd="true"

            />

        <Button
            android:layout_width="120dp"
            android:layout_height="40dp"
            android:background="@drawable/custombtn18"
            android:onClick="playSound17"
            android:id="@+id/button8"
            android:layout_below="@+id/button6"
            android:layout_alignParentEnd="true"

            />


    </RelativeLayout>
    <ListView
        android:id="@android:id/list"
        android:background="@android:color/white"
        android:layout_width="305dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>


</LinearLayout>

Code for the activity that crashes xml is pretty much the same as the main activity xml apart from different buttons and background.

xml for mrsmith:

<LinearLayout 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"
tools:context=".MyActivity"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/primary"
    app:theme="@style/ToolbarTheme"
    app:popupTheme="@style/Theme.AppCompat"/>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/smithee">


        <Button
            android:layout_width="170dp"
            android:layout_height="100dp"
            android:background="@drawable/custombtn23"
            android:onClick="playSound19"
            android:id="@+id/button16"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true" />
        <Button
            android:layout_width="170dp"
            android:layout_height="100dp"
            android:background="@drawable/custombtn24"
            android:onClick="playSound20"
            android:id="@+id/button17"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true" />
        <Button
            android:layout_width="170dp"
            android:layout_height="100dp"
            android:background="@drawable/custombtn22"
            android:onClick="playSound18"
            android:id="@+id/button15"
            android:layout_below="@+id/button17"
            android:layout_alignParentEnd="true" />
        <Button
            android:layout_width="170dp"
            android:layout_height="100dp"
            android:background="@drawable/custombtn27"
            android:onClick="playSound23"
            android:id="@+id/button20"
            android:layout_below="@+id/button16"
            android:layout_alignParentStart="true" />
        <Button
            android:layout_width="170dp"
            android:layout_height="100dp"
            android:background="@drawable/custombtn25"
            android:onClick="playSound21"
            android:id="@+id/button18"
            android:layout_below="@+id/button20"
            android:layout_alignParentStart="true" />
        <Button
            android:layout_width="170dp"
            android:layout_height="100dp"
            android:background="@drawable/custombtn26"
            android:onClick="playSound22"
            android:id="@+id/button19"
            android:layout_below="@+id/button15"
            android:layout_alignStart="@+id/button15" />
        <Button
            android:layout_width="170dp"
            android:layout_height="100dp"
            android:background="@drawable/custombtn28"
            android:onClick="playSound24"
            android:id="@+id/button21"
            android:layout_below="@+id/button18"
            android:layout_alignParentStart="true" />
        <Button
            android:layout_width="170dp"
            android:layout_height="100dp"
            android:background="@drawable/custombtn29"
            android:onClick="playSound25"
            android:id="@+id/button22"
            android:layout_alignTop="@+id/button21"
            android:layout_alignStart="@+id/button19" />
        <Button
            android:layout_width="170dp"
            android:layout_height="100dp"
            android:background="@drawable/custombtn30"
            android:onClick="playSound26"
            android:id="@+id/button23"
            android:layout_below="@+id/button21"
            android:layout_centerHorizontal="true" />

    </RelativeLayout>
    <ListView
        android:id="@android:id/list"
        android:background="@android:color/white"
        android:layout_width="305dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>


</LinearLayout>

If you need any more code let me know. Am not sure what the problem is.

Rohrer answered 31/3, 2015 at 9:48 Comment(3)
can you post XML of MrSmith class ?Unseam
can you tell me what is at line 85 of the XML file of MrSmith class ?Unseam
I think.. for this custom position of Back button you can use layer-list drawable.Selfrestraint
U
0

Indeed, the problem seems to be the low size of your emulator's RAM. You have used some heavy drawables as backgrounds for your Buttons in the navigation drawer. The OutOfMemory error seems to be thrown due to low RAM availabilty, because of one of these large-sized images.

Go to the AVD manager and increase the size of your RAM to at least 512 MB and then try running the app again. Also try testing on a real device if one is available.

Unseam answered 31/3, 2015 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.