0

我有一个片段活动,它为选定的单个记录加载一整行,第一次工作正常,但如果我改变方向,它会崩溃并抛出“找不到源”。当我逐行调试时,一些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;
    } 
4

1 回答 1

0

我相信你是说当方向改变时你的代码不会运行。

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

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

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

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

于 2012-10-02T15:31:09.677 回答