我已经使用FragmentStatePagerAdapter来破坏碎片,但是我的应用程序仍然崩溃 - 当我滑动到我的第3个碎片然后应用程序崩溃时,内存监视器显示内存使用量的大峰值。我总共有5个片段,因为我正在尝试构建一个声卡应用程序。每个片段都是不同的音板。
这是我的mainactivity.java
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.pager);
PagerAdapter padapter = new PagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(padapter);
viewpager.setOffscreenPageLimit(0);
}
}
这是我的pageradapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
case 3:
return new FragmentFour();
case 4:
return new FragmentFive();
default:
break;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 5;
}
}
这是我的第一个片段:
public class FragmentOne extends Fragment {
SoundPool Clubb1;
int clubb1Id;
SoundPool Clubb2;
int clubb2Id;
SoundPool Clubb3;
int clubb3Id;
SoundPool Clubb4;
int clubb4Id;
SoundPool Clubb5;
int clubb5Id;
SoundPool Clubb6;
int clubb6Id;
SoundPool Clubb7;
int clubb7Id;
SoundPool Clubb8;
int clubb8Id;
SoundPool Clubb9;
int clubb9Id;
SoundPool Clubb10;
int clubb10Id;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1, 1);
Clubb2 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb2Id = Clubb2.load(getActivity(), R.raw.clubb2, 1);
Clubb3 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb3Id = Clubb3.load(getActivity(), R.raw.clubb3, 1);
Clubb4 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb4Id = Clubb4.load(getActivity(), R.raw.clubb4, 1);
Clubb5 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb5Id = Clubb5.load(getActivity(), R.raw.clubb5, 1);
Clubb6 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb6Id = Clubb6.load(getActivity(), R.raw.clubb6, 1);
Clubb7 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb7Id = Clubb7.load(getActivity(), R.raw.clubb7, 1);
Clubb8 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb8Id = Clubb8.load(getActivity(), R.raw.clubb8, 1);
Clubb9 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb9Id = Clubb9.load(getActivity(), R.raw.clubb9, 1);
Clubb10 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb10Id = Clubb10.load(getActivity(), R.raw.clubb10, 1);
View rootView = inflater.inflate(R.layout.fragment_one_layout, container, false);
Button buttonA = (Button) rootView.findViewById(R.id.buttonA);
Button buttonB = (Button) rootView.findViewById(R.id.buttonB);
Button buttonC = (Button) rootView.findViewById(R.id.buttonC);
Button buttonD = (Button) rootView.findViewById(R.id.buttonD);
Button buttonE = (Button) rootView.findViewById(R.id.buttonE);
Button buttonF = (Button) rootView.findViewById(R.id.buttonF);
Button buttonG = (Button) rootView.findViewById(R.id.buttonG);
Button buttonH = (Button) rootView.findViewById(R.id.buttonH);
Button buttonI = (Button) rootView.findViewById(R.id.buttonI);
Button buttonJ = (Button) rootView.findViewById(R.id.buttonJ);
View.OnClickListener ocl = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.buttonA:
//A clicked
Clubb1.play(clubb1Id, 1, 1, 1, 0, 1);
break;
case R.id.buttonB:
//B clicked
Clubb2.play(clubb2Id, 1, 1, 1, 0, 1);
break;
case R.id.buttonC:
//C clicked
Clubb3.play(clubb3Id, 1, 1, 1, 0, 1);
break;
case R.id.buttonD:
//D clicked
Clubb4.play(clubb4Id, 1, 1, 1, 0, 1);
break;
case R.id.buttonE:
//E clicked
Clubb5.play(clubb5Id, 3, 3, 1, 0, 1);
break;
case R.id.buttonF:
//F clicked
Clubb6.play(clubb6Id, 1, 1, 1, 0, 1);
break;
case R.id.buttonG:
//G clicked
Clubb7.play(clubb7Id, 1, 1, 1, 0, 1);
break;
case R.id.buttonH:
//H clicked
Clubb8.play(clubb8Id, 1, 1, 1, 0, 1);
break;
case R.id.buttonI:
//I clicked
Clubb9.play(clubb9Id, 1, 1, 1, 0, 1);
break;
case R.id.buttonJ:
//J clicked
Clubb10.play(clubb10Id, 1, 1, 1, 0, 1);
break;
}
}
};
buttonA.setOnClickListener(ocl);
buttonB.setOnClickListener(ocl);
buttonC.setOnClickListener(ocl);
buttonD.setOnClickListener(ocl);
buttonE.setOnClickListener(ocl);
buttonF.setOnClickListener(ocl);
buttonG.setOnClickListener(ocl);
buttonH.setOnClickListener(ocl);
buttonI.setOnClickListener(ocl);
buttonJ.setOnClickListener(ocl);
return rootView;
}}
任何想法如何阻止此内存峰值和应用程序崩溃?当我用3个片段尝试它时,应用程序工作得很好,并没有崩溃或滞后。现在我已经添加了两个碎片,当你滑动到达第三个或第四个碎片并且从不加载第五个碎片时,应用程序崩溃了。它不会让你滑到那么远。我真的迷路了,不知道该怎么做。
答案 0 :(得分:0)
您对SoundPool类的使用看起来并不好看。看看下面的教程; http://www.101apps.co.za/articles/using-android-s-soundpool-class-a-tutorial.html
SoundPool背后的理念是,您可以在同一个池中加载多个样本。您的整个应用程序可能应该有一个池;或每个片段最多一个。
由于SoundPool对象是原生的,因此您还必须注意手动调用" release"在他们。官方文件说
在我们的例子中,当玩家完成了关卡时,游戏 逻辑应该调用SoundPool.release()来释放所有本机 正在使用的资源,然后将SoundPool引用设置为null。如果 玩家开始另一个级别,创建一个新的SoundPool,声音是 加载,然后播放简历。
您可以在Fragment的onCreate()方法中创建SoundPool,并在onDestroy()中调用release
您得到了OutOfMemoryError,因为您已将viewpager setOffscreenPageLimit设置为0,这意味着永远不会缓存片段实例。每次向左或向右滑动时,都会创建一个新的片段,其中包含10个永不释放的新SoundPool对象。