由低可用RAM引起的奇怪行为/崩溃?

时间:2014-08-05 19:11:27

标签: android memory

我正忙着为无线电控制的业余/职业比赛世界开发一个“简单”的应用程序。这个应用程序包含很多静态文本视图,图像视图,表格等。为了显示这些信息,我使用了一个viewpager。每个新页面都是一个片段,每个片段都有自己的静态布局。例如,每个片段包含大约50个视图(我的帖子太长,所以我把它放在一个文本文件中,以便你可以下载它): https://www.dropbox.com/s/cxg8tsutzu6lndw/fragment.txt

我处理这些片段的代码如下所示:

public class SubScreenHandler extends FragmentActivity{
    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sub_screen_handler_layout);
        ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
        fragmentList.addAll((ArrayList<Fragment>) getIntent().getSerializableExtra("fragmentList"));
        for (Object item : fragmentList) {
            if (!(item instanceof Fragment)) {
                throw new ClassCastException("List contained non-Fragment objects: " + item.getClass().getCanonicalName());
            }
        }
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setAdapter(new myPagerAdapter(getFragmentManager(), fragmentList));
        viewPager.setOffscreenPageLimit(1);
        UnderlinePageIndicator underlinePageIndicator = (UnderlinePageIndicator) findViewById(R.id.indicator);
        underlinePageIndicator.setViewPager(viewPager);
        doFirstRun();
    }

    private void doFirstRun(){
        SharedPreferences sharedPreferences=getSharedPreferences("com.example.offroad_set_up_book", Context.MODE_PRIVATE);
        if(sharedPreferences.getBoolean("isFirstRun", true)){
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putBoolean("isFirstRun", false);
            editor.commit();
            AlertDialog.Builder builder=new AlertDialog.Builder(this);
            builder.setMessage(getString(R.string.alertDialog_swipe_function));
            builder.setCancelable(false);
            builder.setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.cancel();
                    }
                });
            AlertDialog alert = builder.create();
            alert.show();
        }
    }
}

class myPagerAdapter extends FragmentStatePagerAdapter {
    ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
    public myPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
        super(fm);
        fragmentList.addAll(fragments);
    }

    @Override
    public Fragment getItem(int i) {
        return fragmentList.get(i);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

}

fragmentList包含在主活动中指定的多个片段(如上所述),然后放入intent。

现在出现问题: 这个应用程序在我的LG G3(3GB内存)上工作正常,这个应用程序在Android模拟器中运行良好,它在我的三星galaxy s3上运行良好。然而在另一款智能手机上(阿尔卡特一触式995,配备512mb的内存)应用确实表现得非常奇怪。首先,并非所有内容都在屏幕上正确显示。像表格的某些部分一样,根本没有显示。最后,当我在片段之间滑动时,我的应用程序崩溃并返回nullpointerexception。 此外,我得到了一些奇怪的记录,我没有得到我的LG G3和我的星系s3:

08-05 20:58:43.682    5722-5722/com.example.offroad_set_up_book D/memalloc﹕ /dev/pmem: Mapped buffer base:0x4b3c3000 size:12165120 offset:10629120 fd:65
08-05 20:58:43.953    5722-5722/com.example.offroad_set_up_book D/memalloc﹕ /dev/pmem: Mapped buffer base:0x4c1e1000 size:13701120 offset:12165120 fd:68
08-05 20:58:43.983    5722-5722/com.example.offroad_set_up_book D/memalloc﹕ /dev/pmem: Mapped buffer base:0x4cef2000 size:21626880 offset:20090880 fd:71
08-05 20:58:43.993    5722-5722/com.example.offroad_set_up_book D/memalloc﹕ /dev/pmem: Unmapping buffer base:0x49b81000 size:6205440 offset:4669440
08-05 20:58:43.993    5722-5722/com.example.offroad_set_up_book D/memalloc﹕ /dev/pmem: Unmapping buffer base:0x4a32c000 size:4669440 offset:3133440
08-05 20:58:43.993    5722-5722/com.example.offroad_set_up_book D/memalloc﹕ /dev/pmem: Unmapping buffer base:0x4a8a0000 size:10629120 offset:9093120

比一些刷卡后我得到了我的nullpointerexception:

08-05 20:59:47.154    5722-5722/com.example.offroad_set_up_book W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a911f8)
08-05 20:59:47.174    5722-5722/com.example.offroad_set_up_book E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1542)
            at android.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1572)
            at android.app.Activity.onSaveInstanceState(Activity.java:1164)
            at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:546)
            at android.app.Activity.performSaveInstanceState(Activity.java:1113)
            at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1185)
            at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:2809)
            at android.app.ActivityThread.handleStopActivity(ActivityThread.java:2867)
            at android.app.ActivityThread.access$900(ActivityThread.java:124)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1162)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4440)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
            at dalvik.system.NativeStart.main(Native Method)

我认为这与alcatel one touch的记忆有关。但是,如果内存不足,为什么垃圾收集器永远不会被调用?在创建viewpager时,GC在我的LG G3和S3上调用一次(也在朋友LG optimus g上)。此外,我从未收到过故障异常。任何人都知道这里发生了什么?

注意: 所有手机至少都在运行android 4.xxx

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。由于我将导入“import android.app.Fragment”,因此发生了奇怪的崩溃。主要活动(将片段放在第二个活动中)并导入“import android.support.v4.app.Fragment;”关于第二项活动。现在一切正常!