我尝试从活动中更改Fragment-UI,但它不起作用。
我在HeaderSrollingViewBehaviour.java中得到一个NullPointerException
是AppBarLayout.java的一部分。
这只发生在我从updateUIonFragmentOverview()调用fragmentOverview.updateUI()时。
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
FragmentOverview fragmentOverview;
String toastOnUIThreadMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialisiert das Feld monitoringActivity im Application-Objekt
//dieses weiß dann, das die Activity gestartet wurde
((BillingSystemApplication) this.getApplicationContext()).setMonitoringActivity(this);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
//initialisieren des FragmentOverviews
fragmentOverview = new FragmentOverview();
tabLayout.setupWithViewPager(viewPager);
updateUIonFragmentOverview();
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(fragmentOverview, "Übersicht");
adapter.addFragment(new FragmentOverview(), "Teilstrecken");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
public void ToastOnUIThread(String message)
{
toastOnUIThreadMessage = message;
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run()
{
Toast.makeText(MainActivity.this, toastOnUIThreadMessage, Toast.LENGTH_LONG).show();
}
});
}
public void updateUIonFragmentOverview()
{
fragmentOverview.updateUI();
}
}
logcat的
FATAL EXCEPTION: main
Process: com.cyberlandgo.felix.workt20, PID: 14147
java.lang.NullPointerException
at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:418)
at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:413)
at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:103)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1003)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1151)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1085)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1611)
at android.view.View.measure(View.java:17331)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5376)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:671)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1319)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:736)
at android.view.View.measure(View.java:17331)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5376)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17331)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5376)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17331)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5376)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:17331)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5376)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17331)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5376)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2548)
at android.view.View.measure(View.java:17331)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2279)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1393)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1592)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6540)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
at android.view.Choreographer.doCallbacks(Choreographer.java:613)
at android.view.Choreographer.doFrame(Choreographer.java:583)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
我只想访问片段UI并进行更改 我希望你能帮助我!