当我打开另一个活动的应用程序崩溃 [英] App crashes when I open another activity

查看:173
本文介绍了当我打开另一个活动的应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我创建5个活动,有其是由5个独立的soundPools控制的5个独立的音板的应用程序。我也使用在每个活动的导航抽屉。当我运行应用程序,它可以打开前两个活动,但每当我打电话第​​三,打开应用程序停止工作。在内存监视内存的使用情况峰64MB,然后崩溃。难道仅仅是模拟器限制为64MB,它会正常工作的电话或我有内存泄漏的地方?我是否需要调用的onStop当我切换到另一个活动,然后ONSTART当其重新打开或者我不需要?如果我做我会怎么做,对于这个项目?

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?

下面是一个链接,查看我的崩溃日志: https://开头www.dropbox.com/s/x5obsexzgi2ofaa/crashlog2.docx?dl=0

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

MainActivity.java类:

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对于崩溃的活动(pretty的多少与上述相同只是不同的声音池和音频文件的称呼)

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

activitymain.xml:

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对于崩溃XML的活动是pretty的大致相同,从不同的按钮和背景的主要活动XML分开。

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

XML进行mrsmith:

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>

如果您需要任何更多的code让我知道。我不知道是什么问题。

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

推荐答案

事实上,这个问题似乎是你的模拟器的RAM的低尺寸。你已经使用了一些重可绘制作为背景为你的按钮 S在导航抽屉里。该内存不足错误似乎由于低内存供应情况被抛出,因为这些大尺寸图像之一。

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.

转至AVD管理,提高您的RAM的大小至少为512 MB,然后尝试运行该应用程序了。也可以尝试在真实设备上测试(如果可用)。

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.

这篇关于当我打开另一个活动的应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆