如何将片段附加到 MainActivity?尝试在空对象引用上调用虚拟方法
How can I attach fragment to MainActivity? Attempt to invoke virtual method on a null object reference
我在尝试执行时遇到错误(尝试在空对象引用上调用虚拟方法)
FragmentThree f = (FragmentThree) getSupportFragmentManager().findFragmentByTag(tag);
f.displayReceivedDataLanLon(message);
这是整体的样子 class
public class MainActivity extends AppCompatActivity implements FragmentOne.SendMessage, FragmentOne.SendMessageLanLon {
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public void sendData(String message) {
String tag = "android:switcher:" + R.id.viewPager + ":" + 1;
FragmentTwo f = (FragmentTwo) getSupportFragmentManager().findFragmentByTag(tag);
f.displayReceivedData(message);
}
@Override
public void sendDataLatLon(String message) {
String tag = "android:switcher:" + R.id.viewPager + ":" + 2;
FragmentThree f = (FragmentThree) getSupportFragmentManager().findFragmentByTag(tag);
f.displayReceivedDataLanLon(message);
}
}
这是 ViewPagerAdapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
if (position == 0) {
fragment = new FragmentOne();
} else if (position == 1) {
fragment = new FragmentTwo();
} else if (position == 2) {
fragment = new FragmentThree();
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
片段管理器找不到我的片段,这可能意味着它还没有被附加。但我不知道我该怎么做,或者这是否是问题所在..
使用
viewPager.setOffscreenPageLimit(2);
这个方法可以解决你的问题。但我建议您尝试扩展 FragmentStatePagerAdapter
而不是 FragmentPagerAdapter
。它将保留您的寻呼机状态。
更新我的测试:
我在 onCreate() 方法中添加了一些代码,一切正常:
viewPager.setOffscreenPageLimit(2);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
sendData("test message");
sendDataLatLon("test message");
}
}, 3000);
可能是sendData
和sendDataLatLon
两个方法在fragment还没有创建的时候被调用了,试试看
我在尝试执行时遇到错误(尝试在空对象引用上调用虚拟方法)
FragmentThree f = (FragmentThree) getSupportFragmentManager().findFragmentByTag(tag);
f.displayReceivedDataLanLon(message);
这是整体的样子 class
public class MainActivity extends AppCompatActivity implements FragmentOne.SendMessage, FragmentOne.SendMessageLanLon {
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public void sendData(String message) {
String tag = "android:switcher:" + R.id.viewPager + ":" + 1;
FragmentTwo f = (FragmentTwo) getSupportFragmentManager().findFragmentByTag(tag);
f.displayReceivedData(message);
}
@Override
public void sendDataLatLon(String message) {
String tag = "android:switcher:" + R.id.viewPager + ":" + 2;
FragmentThree f = (FragmentThree) getSupportFragmentManager().findFragmentByTag(tag);
f.displayReceivedDataLanLon(message);
}
}
这是 ViewPagerAdapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
if (position == 0) {
fragment = new FragmentOne();
} else if (position == 1) {
fragment = new FragmentTwo();
} else if (position == 2) {
fragment = new FragmentThree();
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
片段管理器找不到我的片段,这可能意味着它还没有被附加。但我不知道我该怎么做,或者这是否是问题所在..
使用
viewPager.setOffscreenPageLimit(2);
这个方法可以解决你的问题。但我建议您尝试扩展 FragmentStatePagerAdapter
而不是 FragmentPagerAdapter
。它将保留您的寻呼机状态。
更新我的测试: 我在 onCreate() 方法中添加了一些代码,一切正常:
viewPager.setOffscreenPageLimit(2);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
sendData("test message");
sendDataLatLon("test message");
}
}, 3000);
可能是sendData
和sendDataLatLon
两个方法在fragment还没有创建的时候被调用了,试试看