ViewPager - Update fragment from activity

2019-07-27 11:49发布

I have a ViewPager with 3 fragments. One fragment downloads data ​from a website and displays it in TextViews. Now when the user clicks in the menu of the MainActivity on the item "refresh", the fragment should redownloads the data and displays it. I tried to create a function refresh in the fragment's class and call it from the MainActivity's menu. That worked, but then it crashes because the app can't find the textview. The variable view is empty and it causes a null pointer exception.

What can I do to resolve this?

Many thanks!

1条回答
家丑人穷心不美
2楼-- · 2019-07-27 12:16

From your fragmentactivity,

Fragment tehFraggerz = getSupportFragmentManager().findFragmentByTag(string);

where string is the tag. You set the tag of the fragments in the add, replace, etc methods.

Once you have your fragment, you can do

TextView tehTexterz = (TextView) tehFraggerz.getView().findViewById(R.id.tehText);
查看更多
登录 后发表回答