Android方向更改,片段活动处理片段

时间:2012-10-02 14:24:05

标签: android eclipse android-fragments

我有一个片段活动,它为所选的单个记录加载一个完整的行,这在第一次正常工作,但是如果我改变方向它会崩溃并抛出“Source not found”。当我逐行调试时,一些TextView不可用(返回null),这是我的片段活动代码:

@Override


public void onCreate(Bundle savedInstanceState)` 
    {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.client_layout);

        if (savedInstanceState==null)
        {
            clientActivityViewModel= (ClientActivityViewModel)getLastCustomNonConfigurationInstance();

            if(clientActivityViewModel==null)
            {
                clientActivityViewModel= new ClientActivityViewModel(getBaseContext());
                if (bundle== null)
                {
                    bundle= getIntent().getExtras();
                }
                clientActivityViewModel.setAccountId(bundle.getString("accountId"));
                clientActivityViewModel.GetAccountInformation(bundle.getString("accountId"));
                clientInformationActivity= new ClientInformationActivity();
                clientInformationActivity.setAccountInformation(clientActivityViewModel.getAccountInformation());

                clientActivityViewModel.GetAccountComplementaryInformation(bundle.getString("accountId"));
                clientComplementaryInformationActivity= new ClientComplementaryInformationActivity();
                clientComplementaryInformationActivity.setAccountComplementaryInformation(clientActivityViewModel.getAccountComplementaryInformation());
            }

            if(clientActivityViewModel!=null)
            {
                clientActivityViewModel.GetAccountInformation(clientActivityViewModel.getAccountId());
                clientInformationActivity= new ClientInformationActivity();
                clientInformationActivity.setAccountInformation(clientActivityViewModel.getAccountInformation());

                clientActivityViewModel.GetAccountComplementaryInformation(clientActivityViewModel.getAccountId());
                clientComplementaryInformationActivity= new ClientComplementaryInformationActivity();
                clientComplementaryInformationActivity.setAccountComplementaryInformation(clientActivityViewModel.getAccountComplementaryInformation());

                if (clientActivityViewModel != null) 
                { 
                    pager = (ViewPager) findViewById (R.id.pager);
                    MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
                    adapter.addFragment(clientInformationActivity);
                    adapter.addFragment(clientComplementaryInformationActivity);
                    adapter.addFragment(new ClientAddressActivity());
                    adapter.addFragment(new ClientPaymentInfoActivity());
                    adapter.addFragment(new ClientDescriptionInfoActivity());
                    pager.setAdapter(adapter);
                } 
            }
        }
    }


    @Override
    public Object onRetainCustomNonConfigurationInstance()
    { 
        if (clientActivityViewModel== null)
        {
            clientActivityViewModel= new ClientActivityViewModel(getBaseContext());
            bundle= getIntent().getExtras();
            clientActivityViewModel.setAccountId(bundle.getString("accountId"));
        }
        return clientActivityViewModel;
    } 

1 个答案:

答案 0 :(得分:0)

我相信你说当方向改变时你的代码没有运行。

如果是这种情况,那么当方向再次改变时,会调用 Oncreate 。这就是为什么你的活动第一次没有正确运行的原因。

要解决此问题,您需要在清单文件中声明:

android:configChanges="keyboardHidden|orientation" 
android:configChanges="keyboardHidden|screensize" 

最后一行仅在android 3.0及以上版本中可用