当我打开另一个 activity 时应用程序崩溃

App crashes when I open another activity

所以我正在创建一个包含 5 个活动的应用程序,这些活动有 5 个独立的音板,其中由 5 个独立的 soundPools 控制。我还在每个 activity 上使用导航抽屉。当我 运行 应用程序时,它可以打开前两个活动,但每当我调用第三个活动来打开应用程序时,它就会停止工作。在内存监视器上,内存使用量达到 64MB,然后崩溃。只是模拟器被限制为 64MB,它可以在 phone 上正常工作,还是我在某处有内存泄漏?当我切换到另一个 activity 时是否需要调用 onStop ,然后在它重新打开时调用 onStart 还是不需要?如果我这样做,我会如何为这个项目做到这一点?

这里是link查看我的崩溃日志: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);}

}

崩溃的 activity 代码(与上面几乎相同,只是调用了不同的声音池和音频文件)

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>

崩溃 xml 的 activity 的代码与主要的 activity xml 几乎相同,除了不同的按钮和背景。

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: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>

如果您需要更多代码,请告诉我。我不确定是什么问题。

的确,问题似乎出在您的模拟器 RAM 太小了。您在导航抽屉中使用了一些沉重的可绘制对象作为 Button 的背景。 OutOfMemory 错误似乎是由于 RAM 可用性低而引发的,因为这些大尺寸图像之一。

转到 AVD 管理器并将 RAM 的大小增加到至少 512 MB,然后再次尝试 运行 该应用程序。如果可用,也请尝试在真实设备上进行测试。