Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在FragmentPagerAdapter内旋转设备后,FindViewById为null_Android_Fragmentpageradapter_Findviewbyid_Screen Rotation_Android Runonuithread - Fatal编程技术网

Android 在FragmentPagerAdapter内旋转设备后,FindViewById为null

Android 在FragmentPagerAdapter内旋转设备后,FindViewById为null,android,fragmentpageradapter,findviewbyid,screen-rotation,android-runonuithread,Android,Fragmentpageradapter,Findviewbyid,Screen Rotation,Android Runonuithread,我正在尝试修改ViewPager上FragmentPagerAdapter中的UI元素。 在我转动设备之前,一切都很顺利。然后,FindViewById返回null,我无法修改元素 当我旋转设备时,我是否必须记住一些操作? FragmentPagerAdapter在活动重新创建时管理片段? runOnUIThread对此有何影响?我用错了什么 MainActivity.java @Override protected void onCreate(Bundle savedInstanc

我正在尝试修改
ViewPager
FragmentPagerAdapter
中的UI元素。 在我转动设备之前,一切都很顺利。然后,
FindViewById
返回
null
,我无法修改元素

当我旋转设备时,我是否必须记住一些操作?
FragmentPagerAdapter
在活动重新创建时管理片段?
runOnUIThread
对此有何影响?我用错了什么

MainActivity.java


 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ...

        //detect if it was recreating by rotation device
        if(savedInstanceState != null) {
            rotateEvent = savedInstanceState.getBoolean("rotateEvent");
        }


        // Set up the ViewPager with the sections adapter.
        mViewPager = findViewById(R.id.pager);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setOffscreenPageLimit(5);


        tabLayout = findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        ...
   }

   /**
     * On Received Status update on UI state
     * @param messageReceived: string received
     */
    public void updateReceivedStatus(final String messageReceived) {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                    int idElementToUpdate = getResources().getIdentifier(messageReceived, "id", getPackageName());

                    View element = findViewById(idElementToUpdate); // element is null after rotation

                    //Switch type send different message
                    if (element instanceof RadioGroup) {
                        //obtain id string of radiobutton
                        String radioButtonId = message.replace(":", "_");

                   ...
            }
        }
   }

提前谢谢

我也有同样的问题。我试过这个,但似乎不起作用,我有同样的问题。我试过这个,但似乎不起作用