簡體   English   中英

我打開其他活動時應用崩潰

[英]App crashes when I open another activity

因此,我正在創建一個包含5個活動的應用程序,這些活動具有5個獨立的soundBoard,其中有5個獨立的soundPools。 我還在每個活動上使用導航抽屜。 當我運行該應用程序時,它可以打開前兩個活動,但是每當我調用第三個活動來打開應用程序時,它就會停止工作。 在內存監視器上,內存使用量達到峰值64MB,然后崩潰。 僅僅是模擬器限制為64MB,並且可以在手機上正常工作還是在某處出現內存泄漏? 當我切換到另一個活動時,是否需要調用onStop,然后在重新打開活動時再調用onStart,還是不需要? 如果這樣做,我將如何為此項目做呢?

這是查看我的崩潰日志的鏈接: https : //www.dropbox.com/s/x5obsexzgi2ofaa/crashlog2.docx?dl=0

MainActivity.java類:

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);}

}

崩潰的活動的代碼(與上面的代碼幾乎相同,只是調用了不同的聲音池和音頻文件)

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崩潰的活動代碼與主要活動xml幾乎相同,不同之處在於按鈕和背景不同。

Mrsmith的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的背景。 由於這些大型圖像之一,RAM可用性低似乎引發了OutOfMemory錯誤。

轉到AVD管理器,將RAM的大小至少增加到512 MB,然后嘗試再次運行該應用程序。 如果可用,也請嘗試在真實設備上進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM