所以我创建了一个包含5个活动的应用程序,这些活动有5个单独的soundBoard,其中有5个独立的soundPools。我还在每个活动上使用导航抽屉。当我运行应用程序时,它可以打开前两个活动,但每当我打电话给第三个打开应用程序停止工作。在内存监视器上,内存使用量达到64MB,然后崩溃。它只是模拟器限制为64MB,它可以在手机上正常工作或我在某处有内存泄漏?当我切换到另一个活动然后onStart重新打开或者我不需要时,是否需要调用onStop?如果我这样做,我将如何为这个项目做到这一点?
以下是查看崩溃日志的链接: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几乎相同。
mrs 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>
如果您需要更多代码,请告诉我们。我不确定是什么问题。
答案 0 :(得分:0)
实际上,问题似乎是模拟器RAM的小尺寸。您已使用一些重型drawable作为导航抽屉中Button
的背景。由于RAM的可用性较低,似乎会抛出OutOfMemory
错误,因为其中一个是大尺寸图像。
转到AVD管理器并将RAM的大小增加到至少512 MB,然后再次尝试运行该应用程序。如果可用,还可以尝试在真实设备上进行测试。