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(":", "_");
...
}
}
}
提前谢谢 我也有同样的问题。我试过这个,但似乎不起作用,我有同样的问题。我试过这个,但似乎不起作用